Do not underestimate the ability to Create InfoPath 2012 forms in InfoPath document libraries, its perhaps not quite as straightforward as you think- is anything ever?! But not to worry, Evoke IT have the solution right here! By following our quick and easy steps you will be programming InfoPath forms in document libraries on no time.

After quizzing a few of our trusted developers, it emerged that the easiest way (in short) is to create and save a dummy form with as few values as possible, then programmatically open this form, update the xml nodes to suit, then save it with a different name.  This has the effect of creating a new InfoPath form with your required values.  When you set the values in the xml nodes however, you will need to make sure that you set both the promoted and non-promoted values.

If you need support from our Aberdeen or Edinburgh SharePoint Team just reach out to us.

The following instructions are based on a document library which has been created by publishing an InfoPath form.

STEP 1: Create a blank InfoPath form in your document library using the standard link on the list screen. Once you have done this, save it using a standard name such as BLANK.  If you are unable to specify a form name at the time of saving,, don’t panic just change the name afterwards. Here is the form for this example:

STEP 2: Open the xml behind the form. You can do this by downloading a copy of the file, then opening it using Notepad++.  As you can see in this example, I have added a repeating section and named the groups appropriately so that you can see how this looks.

1. Using the XML structure in the document, write code to open the dummy form and set the values of the nodes.  Be sure to add a using statement for System.Xml. Namespaces can often cause a problem when setting values, and if you find that your values just won’t set, it’s probably down to namespaces, and there may be many namespaces referenced from within one form.  As you can see in this example, the namespace “my” is defined at the top of the code, and used throughout.  You can manage this using the XmlNamespaceManager class:

nsmgr = new XmlNamespaceManager(_thisDoc.NameTable);                                                                                                                                                                                                                                                                                                                                   string myNamespace = root.GetAttribute(“xmlns:my”);                                                                                                                                                                                                                                                                                                                            nsmgr.AddNamespace(“my”, myNamespace);

This XmlNamespaceManager instance can then be used as follows:

string xPathText = String.Concat(“child::”, nodeName);                                                                                                                                                                                                                                                                                                                                            XmlNode thisNode = root.SelectSingleNode(xPathText, nsmgr);                                                                                                                                                                                                                                                                                                                                      if (thisNode != null) { thisNode.InnerText = newValue; }

2. Add code to save the form with a new name.  This bit is fairly standard – you need to get the SPFileCollection, and use the “Add” method to save your InfoPath form:

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();SPContext.Current.Web.AllowUnsafeUpdates = true;                                                                                                                                                                                                                                                                                                                                                  SPFile newFile = thisFC.Add(“MyNewDoc”, (encoding.GetBytes(_thisDoc.OuterXml)), true);                                                                                                                                                                                                                   SPContext.Current.Web.AllowUnsafeUpdates = false;


3. After you have completed this, you should have created a new InfoPath form in your document library with the name you have chosen.

Get Up To Speed With SharePoint 2019!

Download Our FREE eBook...


Download SharePoint 2019 eBook