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 update 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. 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=’Update’><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>



Thank You. Just What I needed.
Good stuff, thanks a million!
Two small things though:
1) The single and double quotes will mess up a copy paste – future readers: do remember to find/replace these.
2) future readers: do remember that the service address (“/_vti_bin/lists.asmx”) must match the url of the site you’re calling it from.
That having said, very clever and well written example!
Eric:I had the same issue. Here’s what I did:1. Put the code into a text file (including the tags).2. Upload it to the Style library (accessible via the All Site Content page).3. Point to the URL of the file on the Content Editor web part.@Comment # 3:This wroekd for me: Go to Appearance -> Chrome Type -> Choose None .Hope this helps.Cheers!