Using REST to Add and Delete List Items in SharePoint 2010

In a previous tip, we learned how to access the list items in SharePoint 2010 Lists using REST, which in SharePoint 2010 is powered by ADO.NET Data Services.

As REST's main requirement is HTTP, this gives you lots of freedom when it comes to platform choice. In fact, any platform that supports HTTP is a candidate; this means web technologies like PHP to plain command line tools and everything in between supporting HTTP. In this tip, a very handy utility called "Curl" is used. Curl is a command line tool for transferring data with URL syntax supporting many protocols including HTTP. You may want to download the Curl binary suited to your operating system as it supports multiple platforms.

One of the main reasons for using this tool is to show the richness of REST and also to show that it's perfectly possible to perform these operations successfully without using any browser as a client application.

Before continuing, you need a SharePoint 2010 List. If you've not already done so, create a new SharePoint 2010 "Custom List" named "Customers" and add two custom columns of type "Single line of text," namely "Name" and "Address," to the list. Also add some entries to the list; you can use the following data as illustrated in the figure below.

Adding an Item into the SharePoint 2010 List

To add a new list item, use the curl command as shown below. Note that regardless of the syntax used by the utility, the main takeaway here is the usage of REST using HTTP. Please note that although the command is shown with parameters separated by newline, you do need to make sure that there is no newline character before running the command. Plus you also want to save the command as a batch so that it can be used in the future.

The "--ntlm" switch is for the authentication type that the server side is application is using, "-u:" is the user credentials, which in this case is left blank which means to use the same credentials under which this utility is running -- in this case the utility was launched under account which has rights to add items on the SharePoint 2010 "Customers" List. "-d" defines the data for the list item ,which in this case in the form of "JSON", next "-H" is the header type which declares that the request is in the form of "JSON", and finally "-X" declares which HTTP verb will be used , which in this case is "POST", this is followed by "URL" to the listdata service point towards the "Customers List", this is important as the "ListData.svc" will be doing all the job on the server side. Also replace the "[SiteCollectionUrl]" token with the absolute URL of your SharePoint site collection, where the "Customers" list is located.

Finally, run the command with the above parameters. Once executed, it will display the XML for the newly created list item as shown below. You can also see the newly created list item title and name.

Also browse to the "Customers List", you will see the newly created list item, this is shown in figure below.

Deleting Items

For deletion of the List Item, the HTTP verb "DELETE" will be used, this is what makes the REST consistent and easy to follow. Before you delete the item you need to get the exact item id of the "Customer" which you want to delete. This can be done by looking at XML from previous example, specifically to the "" element in the start; this is illustrated in figure below, with "" element highlighted. Please note that in its completely fine that value of element "" is different in your case. Just copy the full value of element "" which is in fact the absolute URL to the list item , as it will be used in the command to delete the list item.

The command to delete the item using curl utility is as follows

"-ntlm" and "-u" switches are used for authentication type and user credentials respectively as described previously; "-X" is used with "DELETE" followed by the URL of the list item you want to delete, which in this case is the "Customer" item created in the previous example. Run the command. Once completed browse to the Customer List; the newly added customer list item with name "Tom" is now deleted.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Networking Solutions

Partners

More Networking