There are times when we want to delete a file in SharePoint. To do this we need to call UpdateListItems web method in the Lists.asmx SharePoint web service. This document goes into the details of how to delete a file present in a form library in SharePoint.
Create InfoPath project:
- Open Visual Studio and create a new InfoPath project.
- Copy the following code into a text editor and save it as UpdateListItemsConfig.xml.
<?xml version="1.0" encoding="utf-8" ?> <UpdateListItemsConfig> <LibraryName>FormLibraryName</LibraryName> <SharepointDelete> <Batch> <Method ID="1" Cmd="Delete"> <Field Name="ID"></Field> <Field Name="FileRef"></Field> </Method> </Batch> </SharepointDelete> </UpdateListItemsConfig> |
Change the red text to be the form library name where the file that needs to be deleted is present.
Add the UpdateListItemsConfig.xml as a secondary data connection:
- Choose Data Connections from the Tools menu, and then click Add.
- In the Data Connection Wizard, select Receive Data, and then click Next.
- Select XML Document, and then click Next.
- Click Browse, locate and select the UpdateListItemsConfig.xml file, click Open, and then click Next.
- Click Finish, click Yes, and then click Close.
Take UpdateListItems data connection:
Follow the steps to add the UpdateListItems data connection to delete the file:
- Go to Tools->DataConnections and Click on "Add" button.
- Select Submit Data and click on "Next" button.
- Select WebService and Click on "Next" button.
- Enter the Webservice address.
for eg: http://<sharepoint server name>/<subsite name>/_vti_bin/lists.asmx?WSDL" - Select UpdateListItems and click on next.
- By default s0:listName will be selected.
- Click on the Modify Icon at the end of Field or group field.
- Select UpdateListItemsConfig in the Data source dropdown.
- Select LibraryName and Click OK.
- Select the Check Box Submit Data as a String.

- Select s0:Updates and click on the Modify Icon at the end of Field or group field.
- Select UpdateListItemsConfig in the Data source dropdown.
- Select Batch node under SharePointDelete node.
- Select XML Subtree, Including selected element as shown in the picture below.

- Click button, enter SharePointDelete as name and Click on Finish.
Add code to call the SharePointDelete data connection:
- Open the following code into the FormCode.cs:
private void deleteFile(string fileName) { thisXDocument.GetDOM("UpdateListItemsConfig").selectSingleNode("/UpdateListItemsConfig/SharepointDelete/Batch/Method/Field[@Name = 'FileRef']").text = fileName; WebServiceAdapter2 deleteFileFromSharepoint = (WebServiceAdapter2)thisXDocument.DataAdapters["SharePointDelete"]; deleteFileFromSharepoint.Submit(); } |
- Pass the complete filepath to the Method for deleting the File
Ex: http://serverName/sites/SharePointDelete/TestDelete.xml