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.

ADVERTISEMENTS

Problem

You have a SharePoint list for which you want to update one or more of its items from within an InfoPath browser form.

Solution

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.

Discussion

You can achieve this functionality as follows:

  1. In InfoPath, create a new Blank browser-compatible form template.
  2. Add a Data Connection to a SharePoint list. Here we'll use a SharePoint list called Fruits and select its ID and Title fields.
  3. On the Data Source task pane, select the secondary data source for the SharePoint list from the Data source drop-down list box.
  4. 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.
  5. Add a Button control to the InfoPath form template.
  6. Double-click the button to open its Properties dialog box.
  7. On the Button Properties dialog box, select Update Form from the Action drop-down, and click OK.
  8. 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.
  9. 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.
  10. On the Add Reference dialog box, select Windows® SharePoint® Services from the list of components on the .NET tab, and click OK.
  11. In the FormCode.cs file, add a using statement for Microsoft.SharePoint.
  12. 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(
          "../@ID", NamespaceManager).Value);

        // Get the connection to the secondary data source     SharePointListQueryConnection conn =
          (SharePointListQueryConnection)DataSources["Fruits"]
          .QueryConnection
        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;
                  item.Update();
                  web.AllowUnsafeUpdates = false;
                }
              }
              web.Close();
            }
          }
          site.Close();
        }
      }
    }
  13. Save your work and build the project.
  14. Give the InfoPath form template full trust.
  15. 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.

 
 Subscribe for updates via RSS or Email

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 of S.Y.M. Wong-A-Ton. 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, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services