Submitting InfoPath data to a SharePoint list
InfoPath data can be stored anywhere, in any database, even in a text file. Sometimes, there is a need to store the data somewhere else other than the library itself. The form that is submitted and saved in Forms library is an XML form and contains data. This data can also be submitted to other locations when user clicks the submit button. In this article, we will see how we can store this data in a SharePoint list.

1. Create a custom list in SharePoint with the following fields:

a. FirstName
b. LastName
c. Address
d. Zip
e. City
f. State
g. Country
h. Phone
i. Fax
j. Web

Data type for all the fields will be "Single line of Text".

2. Open "user registration" form in editor.

3. Open VSTA

4. Add reference to "Microsoft.SharePoint.DLL".

There are some steps that you need to take before adding the real code. We covered these steps in previous articles but just in case, if you landed directly on this page, here are the steps again:

5. Add a event handler in "InternalStartup()" function

EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);


6. Add the following in the class:

private string LibLocation




return (string)FormState["LibLocation"];




FormState["LibLocation"] = value;



private string Location




return (string)FormState["Location"];




FormState["Location"] = value;



 7. Add following code in the FormEvents_Loading():

//Edit mode

if (e.InputParameters.ContainsKey("XmlLocation"))


Location = e.InputParameters["Source"].ToString();

Location = Location.Substring(0, Location.IndexOf("/", 8));

LibLocation = Location + e.InputParameters["XmlLocation"].ToString();




//New mode

LibLocation = e.InputParameters["SaveLocation"].ToString();


This code defines location for the form submission, that is, the URL where the form would be submitted.

8. Add following code in the "Submit_Clicked()":

XPathNavigator fname = this.CreateNavigator().SelectSingleNode("/my:myFields/my:FirstName", this.NamespaceManager);

XPathNavigator lname = this.CreateNavigator().SelectSingleNode("/my:myFields/my:LastName", this.NamespaceManager);

XPathNavigator address = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Address", this.NamespaceManager);

XPathNavigator zip = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Zip", this.NamespaceManager);

XPathNavigator city = this.CreateNavigator().SelectSingleNode("/my:myFields/my:City", this.NamespaceManager);

XPathNavigator state = this.CreateNavigator().SelectSingleNode("/my:myFields/my:State", this.NamespaceManager);

XPathNavigator country = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Country", this.NamespaceManager);

XPathNavigator phone = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Phone", this.NamespaceManager);

XPathNavigator fax = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Fax", this.NamespaceManager);

XPathNavigator web = this.CreateNavigator().SelectSingleNode("/my:myFields/my:Web", this.NamespaceManager);

SPWeb webroot = null;



webroot = SPContext.Current.Web;



using (SPSite site = new SPSite(webroot.Site.ID))


using (SPWeb localweb = site.OpenWeb(webroot.ID))


SPList list = localweb.Lists["User List"];

SPListItem listItem = list.Items.Add();

localweb.AllowUnsafeUpdates = true;

listItem["Title"] = fname.Value.ToString();

listItem["FirstName"] = fname.Value.ToString();

listItem["LastName"] = lname.Value.ToString();

listItem["Address"] = address.Value.ToString();

listItem["Zip"] = zip.Value.ToString();

listItem["City"] = city.Value.ToString();

listItem["State"] = state.Value.ToString();

listItem["Country"] = country.Value.ToString();

listItem["Phone"] = phone.Value.ToString();

listItem["Fax"] = fax.Value.ToString();

listItem["Web"] = web.Value.ToString();


localweb.AllowUnsafeUpdates = false;




FileSubmitConnection SubmitConnection = (FileSubmitConnection)this.DataConnections["Main submit"];

SubmitConnection.FolderUrl = LibLocation.Substring(0, LibLocation.LastIndexOf("/"));


This code will get data from the form and submit it to the SharePoint list. First we create XPathNavigator objects for each of the form fields. We read value from each field. Inserting data into a SharePoint list seems to be simple but there are some caveats that you need to be aware of.  "SPWeb webroot = null" should be declared outside the try ... catch block. You cannot define it inside the "using" statement. It will throw an error.  It is important to use "RunWithElevatedPrivileges" to avoid the permissions exception. If you don't declare "webroot" object outside the try ... catch block, you get an "Operation is not valid due to the current state of the object." error. In the end, define an object of "FileSubmitConnection" type and use the "Execute()" method to submit the form to the library.

Download completed InfoPath form and application code (ZIP format)

Happy programming!