In this post we will see how to Delete multiple items from SharePoint list items with ECMAScript (aka javascript client object model)
Delete multiple item from SharePoint list.
The following example can help you to understand the request/response model. The following code snippet uses JSOM to retrieve an item based on its ID. It returns the current client context and then calls the executeQueryASync method, specifying the callback methods for success and failure of the operation
function DeleteCrossChargeTransactions(vReferenceId)
{
try
{
var contextDelete = new SP.ClientContext.get_current();
var webDelete = contextDelete.get_web();
var listDelete = webDelete.get_lists().getByTitle('XXX');
var queryDelete = new SP.CamlQuery();
queryDelete.rowLimit=100;
queryDelete.set_viewXml('<View Scope=\'RecursiveAll\'><Query><Where><Eq><FieldRef Name="Reference_x0020_Id" /><Value Type="Text">'+vReferenceId+'</Value></Eq></Where></Query></View>');
// add your query to the getItems command for your list
this.collListItemsDelete = listDelete.getItems(queryDelete);
// issue the load command, these can be batched for multiple operations
contextDelete.load(collListItemsDelete);
// execute the actual query against the server, and pass the objects we have created to either a success or failure function
contextDelete.executeQueryAsync(Function.createDelegate(this, this.SuccessFunction), Function.createDelegate(this, this.failed));
}
catch(e)
{
}
}
function SuccessFunction()
{
var itemsToDelete = new Array();
var count = this.collListItemsDelete.get_count();
var listEnumerator = this.collListItemsDelete.getEnumerator();
var contextDelete= new SP.ClientContext.get_current();
while (listEnumerator.moveNext()) {
var item = listEnumerator.get_current();
itemsToDelete.push(item);
}
for (var i = itemsToDelete.length-1; i >= 0 ; i--) {
itemsToDelete[i].deleteObject();
}
contextDelete.executeQueryAsync(Function.createDelegate(this, this.allItemsDeleted), Function.createDelegate(this, this.failed));
}