Delete multiple items in batch using SharePoint 2013 default services

InfoPath alternatives for form designing SharePoint

In this post we will discuss how to delete multiple items in batch using SharePoint 2013 default services. have to delete multiple documents/items we can use REST api but we have to make ‘n’ number of calls to delete ‘n’ number of Rows. Major performance hit I believe. Here we can easily use SharePoint default Lists.asmx Service to delete multiple rows.

Also check out:

– SharePoint 2010: Show Welcome message session wise Using jQuery

– Cascading dropdownlist in SharePoint 2010 list using jQuery

– Backup and Restore Site Collections in SharePoint 2010 using PowerShell

We all have been using REST api to do different types of CRUD operations but their is also a another way to do the CRUD operations . All we need is to use SharePoint default Services like:
1. Copy.asmx
2. Lists.asmx
3. Versions.asmx
4. UserGroup.asmx
5. Permissions.asmx
6. Views.asmx and many more.

Why to use ?
1. It is already prewritten and all we have to do is just consume it .
2. No overhead of service deployment .
3. Easily you can perform BATCH operations .

if(location.href.indexOf(“TestPage-for-Batch-Operations.aspx”)>-1) {
$(document).ready(function () {

$(“#testBatch”).click(function(){
delete(siteurl);
});
});
}
function delete(siteurl) {
//let say I want to delete item Ids 465 , 466 and 486
var obj=[];
obj.push(465);
obj.push(466);
obj.push(486);
var fields = “”;

//framing the Rows to be deleted .Here I have deleted w.r.t ID but it can be any column name
for(var i=0;i<obj.length;i++){
fields += “<Method ID=\””+obj[i]+”\” Cmd=\”Delete\”><Field Name=\”ID\”>” + obj[i]+ “</Field></Method>”;
}

var batch = “<Batch OnError=\”Continue\”>” + fields + “</Batch>”;
var soapEnv = BuildSoapEnv(batch, ‘Master Document Transmittals’);
DeleteBatchItems(soapEnv, onSuccessDelete,siteurl);

}
//Bulid SOAP Envelope having the full request
function BuildSoapEnv(batch, listname)
{
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>” + listname + “</listName> \
<updates> \
” + batch + “</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>”;
return soapEnv;
}
//Perform Delete operation in a Batch
function DeleteBatchItems(soapEnv, functionToCall,siteurl)
{
$.ajax({
url: siteurl+ “/_vti_bin/Lists.asmx”,
beforeSend: function(xhr) {
//to avoid security validation error SOAPAction header must be provided
xhr.setRequestHeader(“SOAPAction”,”http://schemas.microsoft.com/sharepoint/soap/UpdateListItems”);},
type: “POST”,
dataType: “xml”,
crossdomain:true,
data: soapEnv,
complete: functionToCall,
contentType: “text/xml; charset=\”utf-8\””
});
}
function onSuccessDelete()
{
// do something
}

Similar SharePoint 2013 Tutorials


Leave a Reply