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.
You have a SharePoint list from which you want to delete one or more items via an InfoPath browser form.
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.
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.
- Right-click the last column of the Repeating Table, select Insert and then Columns to the Right from the context menu that appears.
Add a Button control to the new empty column in the Repeating Table.
Figure 1. The InfoPath form template in Design mode.
- On the Data Source task pane, select Main from the Data source drop-down list box, right-click the myFields node, and click Add.
- On the Add Field or Group dialog box, type itemID in the Name field, and click OK.
- Double-click the button to open its Properties dialog box.
- On the Button Properties dialog box, click Rules.
- On the Rules dialog box, click Add.
On the Rule dialog box, click Add Action, and add an action that says:
Set a field's value: itemID = current()/@ID
- Click OK to close all dialog boxes except for the Button Properties dialog box.
- On the Button Properties dialog box, and 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 statement for Microsoft.SharePoint.
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()
// Get a reference to the secondary data source
SharepointListQueryConnection conn =
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;
web.AllowUnsafeUpdates = false;
// Refresh the secondary data source
- 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 and click the Delete button on one of the list items, that item will be deleted from the SharePoint list.
Related InfoPath Articles:
- Automatically add a new item to a SharePoint list using InfoPath 2007 and a custom workflow
- 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
- Use a SharePoint event handler and the object model to submit data from an InfoPath form to a list
- Bulk delete SharePoint list items via an InfoPath 2010 form (no code)
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.