How to update a SharePoint list via an InfoPath browser form using the SharePoint Object Model
This article explains how you can use the SPSite, SPWeb, SPList, and SPListItem classes in C# code in an InfoPath form to update values within a SharePoint list.
You have a SharePoint list for which you want to update one or more of its items from within an InfoPath browser form.
Write code in an event handler for the InfoPath form that makes use of the SharePoint object model to update items in the SharePoint list.
You can achieve this functionality as follows:
- In InfoPath, create a new Blank browser-compatible form template.
- Add a Data Connection to a SharePoint list. Here we'll use a SharePoint list called Fruits and select its ID and Title fields.
- On the Data Source task pane, select the secondary data source for the SharePoint list from the Data source drop-down list box.
- On the Data Source task pane, expand all of the nodes for the secondary data source, and then drag the repeating node for the SharePoint list to the InfoPath form template, drop it, and select Repeating Table from the context menu that appears.
- Add a Button control to the InfoPath form template.
- Double-click the button to open its Properties dialog box.
- On the Button Properties dialog box, select Update Form from the Action drop-down, and click OK.
- Right-click the field for the Title column in the Repeating Table for the secondary data source of the SharePoint list, select Programming, and then Changed Event from the context menu that appears.
- 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 statement for Microsoft.SharePoint.
Add the following C# code to the Changed event handler for the Title field:
if (e.Operation == XmlOperation.ValueChange)
if (e.Site.Name == "Title" && e.OldValue != e.NewValue)
// Get the ID of the item to be changed
int id = Int32.Parse(e.Site.SelectSingleNode(
// Get the connection to the secondary data source SharePointListQueryConnection conn =
Uri siteUri = conn.SiteUri;
// Update the SharePoint list item
using (SPSite site = new SPSite(siteUri.AbsoluteUri))
if (site != null)
using (SPWeb web = site.OpenWeb(siteUri.AbsolutePath))
SPList list = web.GetList(
siteUri.AbsolutePath + "Lists/Fruits");
if (list != null)
SPListItem item = list.GetItemById(id);
if (item != null)
item["Title"] = e.NewValue;
web.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = false;
- 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, change one or more titles for list items, and then click the Update Form button, all of the items you changed should get updated in the 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
- SharePoint list CRUD operations using an InfoPath repeating table
- Automatically add a new item to a SharePoint list using InfoPath 2007 and a custom workflow
- Use a SharePoint event handler and the object model to submit data from a 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.