HOME ABOUT
I AM HERE
  • Twitter icon
  • Facebook icon
  • Technorati icon
Bookmark and Share

Delete all items in a SharePoint list more efficiently

May 24, 2009 17:43 by aidan

One way to remove all items from a SharePoint list is to iterate through every item and call delete like this.

However, a more efficient way to clear down an entire list is to use the ProcessBatchData method like this:

private void deleteAllListItems(SPSite site, SPList list)

  StringBuilder sbDelete = new StringBuilder();

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


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

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

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

  site.RootWeb.ProcessBatchData(sbDelete.ToString());
}


Tags:
Categories:
Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed
blog comments powered by Disqus