0

I need to connect to SharePoint and download InfoPath form data programmatically so that I can schedule the download as an automated process.

I have found information on how to get the form data as XML from the GUI. But I can not find any information on how to get the data from code.

I do not need anything fancy, I need to pull all the data from a particular form.

I found this How To get Data from a Completed InfoPath Form and an answer mentions in passing You have to first extract the underlying XML files, which you could do programmatically, but I have not been able to find any information on how to actually accomplish this.

1

Well there are two types of InfoPath data.

First type is non-promoted InfoPath fields. These are fields in your form that are NOT promoted to SharePoint. The data for these fields live ONLY in the XML file.

The second type are promoted InfoPath fields. These are fields that are in the form that you HAVE decided to promote to SharePoint as columns. The data for these fields live BOTH in the XML and in SharePoint.

So, if all of your fields are the type 2, then you should be able to read the data using the normal SharePoint OM like you would for a list or document library.

SPFile file = web.GetFile("site relative url to file");
SPListItem item = file.item;
string data1 = item["FieldName1"];

If you have any of the type 1 fields, then the best way is to programmatically get the file contents as a string, then load that data into an xmldocument and traverse the document or use xpath queries to get the data you need.

SPFile file = web.GetFile("site relative url to file");
string fileContent = string.Empty;
using (StreamReader sr = new StreamReader(file.OpenBinaryStream()))
{
    fileContent = sr.ReadToEnd();
}

XmlDocument xml = new XmlDocument();
xml.LoadXml(fileContent);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy