Thursday, August 7, 2008

Deleting all the Items from a list or library

Scenario:
You want to delete all the items from a list or library

Code:

StringBuilder sbDelete = new StringBuilder();

sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

sbDelete.Append("<Batch>");

string command = "<Method><SetList Scope=\"Request\">" + spList.ID + "</SetList>";
command +="<SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";

foreach (SPListItem item in spList.Items)
{
sbDelete.Append(string.Format(command, item.ID.ToString()));
}

sbDelete.Append("</Batch>");

spSite.RootWeb.ProcessBatchData(sbDelete.ToString());

0 comments: