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.

Problem

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.

Solution

You could use one of the following 5 methods:

  1. Use the SharePoint Lists web service together with a CAML batch update to add an item to a SharePoint list.
  2. Use a SharePoint Designer workflow to get data from the InfoPath form and create a SharePoint list item.
  3. 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.
  4. 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.
  5. 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.

Discussion

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.

  1. In InfoPath, create a new Blank browser-compatible form template.
  2. Add a Text Box control (field1) and a Button control to the InfoPath form template.
  3. Double-click the button to open its Properties dialog box.
  4. On the Button Properties dialog box, change the Label to Add Item, and then click Edit Form Code.
  5. 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.
  6. On the Add Reference dialog box, select Windows® SharePoint® Services from the list of components on the .NET tab, and click OK.
  7. In the FormCode.cs file, add a using or Imports statement for Microsoft.SharePoint.
  8. 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();
            item["Title"] =
              MainDataSource.CreateNavigator().SelectSingleNode(
              "//my:field1", NamespaceManager).Value;
            item.Update();
          }

          // Turn off AllowUnsafeUpdates on the site
          web.AllowUnsafeUpdates = false;

          // Close the connection to the site
          web.Close();
        }

        // Close the connection to the site collection
        site.Close();
      }
    }

    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") = _
              MainDataSource.CreateNavigator().SelectSingleNode( _
              "//my:field1", NamespaceManager).Value
            item.Update()
          End If

          ' Turn off AllowUnsafeUpdates on the site
          web.AllowUnsafeUpdates = False

          ' Close the connection to the site
          web.Close()

        End Using

        ' Close the connection to the site collection
        site.Close()

      End If

    End Using
  9. Save your work and build the project.
  10. Give the InfoPath form template full trust.
  11. 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.

 
 Subscribe for updates via RSS or Email

Related InfoPath Articles:

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 4: 101 Code Recipes for VB Developers

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