Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
How to bulk delete List Items in SharePoint (using ProcessBatchData) - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

2015-03-10

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());
   }
});

 

(Visited 408 times, 1 visits today)

About the author 

Ashutosh Mund

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.