There are times when we have a list of items that need to be deleted from a SharePoint list and we do not want to loop it one by one and delete it for performance reason. There is a better way to deal with these type of scenario.
The method which is used for this is ProcessBatchData.
See the below code snippet how to use this:
SPSite_site =SPContext.Current.Site;
SPWeb_web = _site.OpenWeb();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPList_list = _web.Lists["ListName"];
SPQuery_query =newSPQuery();
_query.Query ="<Where><Eq><FieldRef Name='PrimaryKeyID' /><Value Type='Text'>"+PrimaryKeyID+"</Value></Eq></Where>";
SPListItemCollection_collection = _list.GetItems(_query);
if(_collection.Count > 0)
{
StringBuildersbDelete =newStringBuilder();
sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
stringcommand ="<Method><SetList Scope=\"Request\">"+ _list.ID +"</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
foreach(SPListItemitemin_collection)
{
sbDelete.Append(string.Format(command, item.ID.ToString()));
}
sbDelete.Append("</Batch>");
_web.ProcessBatchData(sbDelete.ToString());
}
});
