REST and SharePoint 2010 Quick Start Guide: Delete List Items based on $filter

 

Table of Contents for this Series 

This next post concerns itself with using HTTP DELETE to remove one or more SharePoint 2010 list items based on the $filter syntax. The JavaScript code is not really that succinct and is based on the AtomPub format for deleting a single list item. The reference for this is here –  http://msdn.microsoft.com/en-us/library/dd931167.aspx

As of this writing I’m not sure if cleaner code can be written but feel free to tweet me and I’ll update this post.

The url protocol (from the link) looks as follows

DELETE /_vti_bin/ListData.svc/Employees(1) HTTP/1.1
If-Match: W/"1"
Accept: application/atom+xml
Host: http://www.contoso.com

This translate into JQuery/JavaScript as

$.ajax({
       type: "DELETE",
          url: url + "(" + Id + ")",
          contentType: "application/atom+xml",
          error: function(xhr) {
                 alert(xhr.status + ‘: ‘ + xhr.statusText);                                   
             }
});

An example url sent would look somewhat as follows – http://win-fsr6dv1c6bt/_vti_bin/ListData.svc/Calendar(1)

Where the list Id = 1 (in this case). We can also leverage the $filter syntax to pull back one or more Id’s then call the $.ajax DELETE method using this code

function DeleteItems(url, filter) {  
      $.getJSON(url + "?$filter=" + filter,function(data) {
           $.each(data.d.results, function(i,item){
           DeleteListItem(url,item.Id);
      });
   });         
}

The code above will return one or more list rows and immediately call a function (I defined) named DeleteListItem. The complete code is as follows

var url = "http://win-fsr6dv1c6bt/_vti_bin/ListData.svc/Calendar";

var filter = "Title eq ‘Team Meeting’";

DeleteItems(url,filter);

function DeleteItems(url, filter) {
   var len=0;  
   $.getJSON(url + "?$filter=" + filter,function(data) {
      $.each(data.d.results, function(i,item){
          DeleteListItem(url,item.Id);
      });
   });         
}

function DeleteListItem(url,Id) {

    $.ajax({
       type: "DELETE",
          url: url + "(" + Id + ")",
          contentType: "application/atom+xml",
          error: function(xhr) {
                 alert(xhr.status + ‘: ‘ + xhr.statusText);                                   
             }
      });

}

The code above refers to a calendar list on my local development server. The first image is the calendar with a team meeting specified.

cal1

This second image is after the JavaScript code is run – of course, the item is removed. If multiple items existed with the same title as I used in $filter then all would have been removed. 

cal2

Using $.each combined with $filter provides for an interesting way to query and delete target records from your list. Very cool indeed!

Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s