How to delete SharePoint list items 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 delete items from a SharePoint list.

ADVERTISEMENTS

Problem

You have a SharePoint list from which you want to delete one or more items via an InfoPath browser form.

Solution

Write code in an event handler for the InfoPath form that makes use of the SharePoint object model to delete items from 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. Right-click the last column of the Repeating Table, select Insert and then Columns to the Right from the context menu that appears.
  6. Add a Button control to the new empty column in the Repeating Table.
    The InfoPath form template in Design mode
    Figure 1. The InfoPath form template in Design mode.

  7. On the Data Source task pane, select Main from the Data source drop-down list box, right-click the myFields node, and click Add.
  8. On the Add Field or Group dialog box, type itemID in the Name field, and click OK.
  9. Double-click the button to open its Properties dialog box.
  10. On the Button Properties dialog box, click Rules.
  11. On the Rules dialog box, click Add.
  12. On the Rule dialog box, click Add Action, and add an action that says:

    Set a field's value: itemID = current()/@ID
  13. Click OK to close all dialog boxes except for the Button Properties dialog box.
  14. On the Button Properties dialog box, and click Edit Form Code.
  15. 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.
  16. On the Add Reference dialog box, select Windows® SharePoint® Services from the list of components on the .NET tab, and click OK.
  17. In the FormCode.cs file, add a using statement for Microsoft.SharePoint.
  18. Add the following C# code to the Clicked event handler for the button:

    // Get the ID of the item where the button was clicked
    int id = Int32.Parse(MainDataSource.CreateNavigator()
      .SelectSingleNode("//my:itemID", NamespaceManager).Value);

    // Get a reference to the secondary data source
    SharepointListQueryConnection conn =
      (SharepointListQueryConnection)DataSources["Fruits"]
      .QueryConnection;
    Uri siteUri = conn.SiteUrl;

    // Delete the 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)
            {
              web.AllowUnsafeUpdates = true;
              item.Delete();
              web.AllowUnsafeUpdates = false;
            }
          }
          web.Close();
        }
      }
      site.Close();
    }

    // Refresh the secondary data source
    conn.Execute();
  19. Save your work and build the project.
  20. Give the InfoPath form template full trust.
  21. 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 and click the Delete button on one of the list items, that item will be deleted from the SharePoint list.

 


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. 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.

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