Posted By

blackf0rk on 12/16/09


Tagged

list delete items sharepoint SPList


Versions (?)

Delete all items in a SharePoint list


 / Published in: C#
 

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

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

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

Report this snippet  

You need to login to post a comment.