Situation:
We have a large list (more than 100.000 items). The request is to delete all the items.
Solution:
We can try to iterate through all items and delete. Of course this approach will generate performance problems. The best solution I found is to use a batch:
private void ClearList(SPList taskList)
{
StringBuilder sbDelete = BuildBatchDeleteCommand(taskList);
taskList.ParentWeb.ProcessBatchData(sbDelete.ToString());
}
/// <summary>
/// Builds a batch string with a list of all the items that are to be deleted.
/// </summary>
/// <param name="spList"></param>
/// <returns></returns>
private static StringBuilder BuildBatchDeleteCommand(SPList spList)
{
StringBuilder sbDelete = new StringBuilder();
sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
string command = "<Method><SetList Scope=\"Request\">" + spList.ID +
"</SetList><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>");
return sbDelete;
}
More details on how to use batch : http://msdn.microsoft.com/en-us/library/cc404818.aspx
No comments:
Post a Comment