Thursday, April 13, 2006

Programmatically generate/update data for an InfoPath Form and save it in a SharePoint Form Library


Here's a message that a SharePoint Solutions student sent to me regarding how to programmatically generate/update data for an InfoPath form and save it in a SharePoint Form Library. Thanks David!

Hi Tony,

Here is the snippet for one technique to parse an InfoPath form stored in a form library. Before using this code I created a form library with the form template containing around 20 text fields. I also created an empty form in the form library based on this template (BLANK.xml). This example opens an empty form (BLANK.xml) from the form library, reads it into an xml document, parses the xml document to update the fields, and then saves the xml document as a new file back to the form library.

// opens the site
SPWeb webSite = new SPSite(txtUrl.Text).OpenWeb();

// gets the blank file to copy
SPFile BLANK = webSite.Folders["Test1"].Files["BLANK.xml"];

// reads the blank file into an xml document
MemoryStream inStream = new MemoryStream(BLANK.OpenBinary());
XmlTextReader reader = new XmlTextReader(inStream);
XmlDocument xd = new XmlDocument();
xd.Load(reader);
reader.Close();
inStream.Close();

//gets the root element from the xml document
XmlElement root = xd.DocumentElement;

//the loop counter is started at 2 to skip past the namespace tags and start at the data fields
for(int index=2; index<root.ChildNodes.Count; index++)
{
//insert code to parse the xml to update the fields
}


// saves the XML Document back as a file
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
SPFile newFile = webSite.Folders["Test1"].Files.Add("Test_Document.xml", (encoding.GetBytes(xd.OuterXml)), true);

XPath queries could also be used to find and update fields in the xml instead of or in addition to looping through the nodes of the xml document. Another alternative to this would be to use the myschema.xsd from the .xsn file and load a strongly typed dataset based on that schema with the xml, update the fields in the dataset, then write the dataset to xml and appending the proper header tags at the top xml to include the InfoPath namespace information. This could also be slightly modified to open an existing form in a SharePoint form library, update the data elements, and save it back to the form library.


David Pearson
Programmer Analyst Intranet Solutions
HNI Corporation

3 comments:

Anonymous said...

Hi! Thanks for a great tip! I tried the code but get a security validation error. Is it required that the form has a full trust security level?

Anonymous said...

Hi
How can we update the data back into the same blank xml file?
If we don't want to create a new file, and if we want to update the same file, what would be the approach?

Shankara Raman said...

Hi,

A very useful post. It was a time saver for me. I tried to use this code. I was able to successfully add an infopath xml file to my forms library. When I try to open the form, i am getting a message "The Form is Closed". Any idea?