Return to Snippet

Revision: 21634
at December 16, 2009 18:03 by blackf0rk


Updated Code
SPList list = web.Lists[ListId];
SPListItemCollection splic = list.Items;
StringBuilder batchString = new StringBuilder();
batchString.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

foreach (SPListItem item in splic)
{
        batchString.Append("<Method>");
        batchString.Append("<SetList Scope=\"Request\">" + Convert.ToString(item.ParentList.ID) + "</SetList>");
        batchString.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");
        batchString.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
        batchString.Append("</Method>");
}

batchString.Append("</Batch>");
SPContext.Current.Web.ProcessBatchData(batchString.ToString());

Revision: 21633
at December 16, 2009 18:03 by blackf0rk


Initial Code
SPList list = web.Lists[ListId];
SPListItemCollection splic = list.Items;
StringBuilder batchString = new StringBuilder();
batchString.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

foreach (SPListItem item in splic)
{
        batchString.Append("<Method>");
        batchString.Append("<SetList Scope=\"Request\">" + Convert.ToString(item.ParentList.ID) + "</SetList>");
        batchString.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");
        batchString.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
        batchString.Append("</Method>");
}

batchString.Append("</Batch>");
web.ProcessBatchData(batchString.ToString());

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

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

Initial Title
Delete all items in a SharePoint list

Initial Tags
list, sharepoint

Initial Language
C#