Monday, December 1, 2014


In last post we saw how to add new item in list. In this post we will learn how to delete item from the list. Delete is one important operation which we perform many times. This is very simple while performing using REST services. The beauty of REST services is the URL remains same and some of part changes. In case of delete you need to provide the ID of item, you first get the ID of item which needs to be deleting.

The REST URL for delete is:
/sites/OnePalSh/_api/web/lists/getbytitle('StudentsList')/items(" + Id +")

The complete code to delete items is:

Function Delete()
 {
Id = $("#txtID").val();
$.ajax({
url: "/sites/OnePalSh/_api/web/lists/getbytitle('StudentsList')/items(" + Id +")",
type: "POST",
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-HTTP-Method": "DELETE"
},
success: function (data) {
self.Students.remove(function(item) { return item.ID == Id });                          
alert("Deleted Successfuly.");
},
error: function (error) {
alert(JSON.stringify(error));
}
});
 }

If you look at the code snippet the method is nothing but POST. No need to worry about data as you are deleting item. Main things to notice are ‘IF-MATCH’ and ‘X-HTTP-Method’ in the headers section. ‘IF-MATCH’ is used for concurrency but in case of delete operation you need not worry and provide it  “*”. Next parameter is ‘X-HTTP-Method’, in case of delete operation it is ‘DELETE’. Now are ready to make REST call.

Happy SharePoint Coding.

0 comments:

Post a Comment