您的位置:首页 > 其它

Efficiently Delete / Purge All Items from a SharePoint List

2012-06-16 11:56 387 查看
If you want to delete or remove all the items in a SharePoint list you need to iterate through each item and perform a delete. Most of the examples on the web iterate through each item to do the delete. The problem there is that each delete action makes
a request to the server and a huge list is going to take a long time. You could always delete the entire list and recreate it but that would mean recreating the structure as well as the guid being changed.

The better method would be to make a single request with all the delete commands batched together as shown in the code sample below.

1: privatestatic StringBuilder BuildBatchDeleteCommand(SPList spList)

2: {

3:     StringBuilder sbDelete = new StringBuilder();

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

5:     string command = "<Method><SetList Scope=\"Request\">" + spList.ID +

6:         "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";

7:

8:     foreach (SPListItem item in spList.Items)

9:     {

10:         sbDelete.Append(string.Format(command, item.ID.ToString()));

11:     }

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

13:     return sbDelete;

14: }


From: http://merill.net/2008/02/efficiently-delete-purge-all-items-from-a-sharepoint-list/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: