How to use the SharePoint object model to submit data from an InfoPath browser form to a SharePoint list
This article explains how you can use the SPContext, SPSite, SPWeb, SPList, and SPListItem classes in C# or Visual Basic code in an InfoPath form to submit the form's data as a new item to a custom SharePoint list.
You want to save the data that is entered into an InfoPath form to a SharePoint list, but InfoPath does not allow you to submit data to a SharePoint list.
You could use one of the following 5 methods:
- Use the SharePoint Lists web service together with a CAML batch update to add an item to a SharePoint list.
- Use a SharePoint Designer workflow to get data from the InfoPath form and create a SharePoint list item.
- When using an InfoPath browser form, call directly into the SharePoint object model from within the InfoPath form to add an item to the SharePoint list.
- Use a SharePoint event handler feature to make calls directly into the SharePoint object model to add an item to the SharePoint list when an InfoPath form is added to a Form Library on which the event handler has been activated.
- Use a custom Visual Studio workflow to add an item to the SharePoint list when an InfoPath form is added to a Form Library on which the workflow has been set to run.
This article discusses method 3.
In the following example, an InfoPath form with one text field is used to submit data to a custom SharePoint list named Fruits that exists in the root site of the site collection and populate the Title column of this list with the data from the text box in InfoPath.
Note: Users must have proper permissions assigned to them to be able to add items to the SharePoint list.
- In InfoPath, create a new Blank browser-compatible form template.
- Add a Text Box control (field1) and a Button control to the InfoPath form template.
- Double-click the button to open its Properties dialog box.
- On the Button Properties dialog box, change the Label to Add Item, and then click Edit Form Code.
- In the Project Explorer window in Microsoft Visual Studio Tools for Applications, right-click the node for the project name, and select Add Reference from the context menu.
- On the Add Reference dialog box, select Windows® SharePoint® Services from the list of components on the .NET tab, and click OK.
- In the FormCode.cs file, add a using or Imports statement for Microsoft.SharePoint.
Add the following C# code to the event handler of the Add Item button:
using (SPSite site = SPContext.Current.Site)
if (site != null)
using (SPWeb web = site.OpenWeb())
// Turn on AllowUnsafeUpdates on the site
web.AllowUnsafeUpdates = true;
// Update the SharePoint list based on the values
// from the InfoPath form
SPList list = web.GetList("/Lists/Fruits");
if (list != null)
SPListItem item = list.Items.Add();
// Turn off AllowUnsafeUpdates on the site
web.AllowUnsafeUpdates = false;
// Close the connection to the site
// Close the connection to the site collection
Or add the following Visual Basic code to the event handler of the Add Item button:
Using site As SPSite = SPContext.Current.Site
If site IsNot Nothing Then
Using web As SPWeb = site.OpenWeb()
' Turn on AllowUnsafeUpdates on the site
web.AllowUnsafeUpdates = True
' Update the SharePoint list based on the values
' from the InfoPath form
Dim list As SPList = web.GetList("/Lists/Fruits")
If list IsNot Nothing Then
Dim item As SPListItem = list.Items.Add()
item("Title") = _
' Turn off AllowUnsafeUpdates on the site
web.AllowUnsafeUpdates = False
' Close the connection to the site
' Close the connection to the site collection
- Save your work and build the project.
- Give the InfoPath form template full trust.
- Publish the InfoPath form template to a SharePoint server running InfoPath Forms Services. Since the InfoPath form template contains code, you will have to perform an administrator-approved deployment.
You should now have a fully functional form so that when you open the InfoPath form in a browser, type a fruit name in the text box, and click the Add Item button, an item should be added to the Fruits custom SharePoint list.
Related InfoPath Articles:
- How to delete SharePoint list items via an InfoPath browser form using the SharePoint Object Model
- How to submit the rows of a repeating table in InfoPath to a SharePoint list
- How to update a SharePoint list via an InfoPath browser form using the SharePoint Object Model
- How to use a SharePoint event handler and the object model to submit data from an InfoPath form to a SharePoint list
- How to use a Visual Studio workflow to submit data from an InfoPath form to a SharePoint list
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.