Delete all items in a SharePoint list


/ Published in: C#
Save to your folder(s)

This will delete all items in a SharePoint list. Better than looping through a SPListItemCollection and doing a .delete();


Copy this code and paste it in your HTML
  1. SPList list = web.Lists[ListId];
  2. SPListItemCollection splic = list.Items;
  3. StringBuilder batchString = new StringBuilder();
  4. batchString.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
  5.  
  6. foreach (SPListItem item in splic)
  7. {
  8. batchString.Append("<Method>");
  9. batchString.Append("<SetList Scope=\"Request\">" + Convert.ToString(item.ParentList.ID) + "</SetList>");
  10. batchString.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");
  11. batchString.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
  12. batchString.Append("</Method>");
  13. }
  14.  
  15. batchString.Append("</Batch>");
  16. SPContext.Current.Web.ProcessBatchData(batchString.ToString());

URL: http://msftplayground.com/archive/2009/03/16/bulk-deletion-of-splistitems-splistitemcollection.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.