Batch delete items with UpdatelistItems SharePoint Web Service + JQuery with CAML query

In Continuation with the Blog series about Working with SharePoint Web Services + JQuery to Add, Update and Delete items in SharePoint list In this post we will see how to batch update items using UpdatelistItems Web Service and JQuery based on a CAML query.

How to batch delete items using UpdatelistItems Web Service based on a CAML query - The code example below makes an Ajax call to GetlistItems web service with CAML query and then creates a batch string from the result set to pass it to UpdatelistItems Web Service. The query is to delete all the items from Announcements list where Title = Test Announcement. Lets look at the code below

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type=”text/javascript”>

function GetAnnouncements(){

var soapEnv = “<soap:Envelope xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’> \
<soap:Body> \
<GetListItems xmlns=’http://schemas.microsoft.com/sharepoint/soap/’> \
<listName>Announcements</listName> \
<query><Query><Where><Eq><FieldRef Name=’Title’/><Value Type=’Text’>Test Announcement</Value> </Eq></Where></Query></query> \
<viewFields> \
<ViewFields> \
<FieldRef Name=’Title’ /> \
<FieldRef Name=’Body’ /> \
<FieldRef Name=’ID’ /> \
</ViewFields> \
</viewFields> \
<rowLimit>99999</rowLimit> \
<queryOptions xmlns:SOAPSDK9=’http://schemas.microsoft.com/sharepoint/soap/’ ><QueryOptions/> \
</queryOptions> \
</GetListItems> \
</soap:Body> \
</soap:Envelope>”;
jQuery.ajax({
url: “/_vti_bin/lists.asmx”,
type: “POST”,
dataType: “xml”,
data: soapEnv,
complete: CreateBatchWithListItems,
contentType: “text/xml; charset=\”utf-8\”"
});
}

function CreateBatchWithListItems(xData, status) {

var oldbatch =”";

jQuery(xData.responseXML).find(“z\\:row”).each(function () {

oldbatch = AddTobatch(oldbatch, $(this).attr(“ows_ID”));

});

UpdateAnnouncement(oldbatch);
}

function AddTobatch(oldbatch, itemId)
{

//Specify the batch query
var _batch = “<Method ID=’1′ Cmd=’Delete’><Field Name=’ID’>” + itemId +”</Field><Field Name=’Title’>Changed Title</Field></Method>”;

oldbatch = oldbatch + _batch;

return oldbatch;
}

function UpdateAnnouncement(batch){

var soapEnv = “<?xml version=\’1.0\’ encoding=\’utf-8\’?> \
<soap:Envelope xmlns:xsi=\’http://www.w3.org/2001/XMLSchema-instance\’ \
xmlns:xsd=\’http://www.w3.org/2001/XMLSchema\’ \
xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/\’> \
<soap:Body> \
<UpdateListItems xmlns=’http://schemas.microsoft.com/sharepoint/soap/’> \
<listName>Announcements</listName> \
<updates> \
<Batch OnError=’Continue’>” + batch + “</Batch> \
</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>”;
$.ajax({
url: “/_vti_bin/lists.asmx”,
beforeSend: function(xhr) { xhr.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/sharepoint/soap/UpdateListItems”);
},
type: “POST”,
dataType: “xml”,
data: soapEnv,
complete: updateResult,
contentType: “text/xml; charset=\”utf-8\”"

});
}

function updateResult(xData, status) {

alert(“Update Complete”);
}

</script>
<a href=”#” onclick=”Javascript:GetAnnouncements();”>Update Announcements with title test</a>

No comments yet.

Leave a Reply