Deleting a large number of items from a list in SharePoint

From :http://blog.thekid.me.uk/archive/2007/02/24/deleting-a-considerable-number-of-items-from-a-list-in-sharepoint.aspx

 

 

Recently the question was asked in the newsgroups about deleting a large number of items from SharePoint (WSS) in the fastest way. I had, in one if my projects, needed to remove a large number of item from SharePoint and the best way I found was to use 'ProcessBatchData' as it avoided the API and was considerably faster.

Here is some example code which will remove items from a SharePoint list. If you do this I would remember about the Recycle Bin and the effect deleting many items will have in the future...it maybe worth adding some code which also removes the items from the recycle bin once it has finished.

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

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

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

try
{
    SPContext.Current.Site.RootWeb.ProcessBatchData(sbDelete.ToString());
}
catch (Exception ex)
{
    
Console.WriteLine("Delete failed: " + ex.Message);
   
throw;
}

I took me some time to re-find this code and so I have posted it here more for my benefit, but if anyone finds it useful then great.

转载于:https://www.cnblogs.com/laputa-sky/archive/2008/09/26/1299867.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值