通过ProcessBatchData()删除列表项

时间:2010-06-14 09:04:47

标签: sharepoint sharepoint-2007

您可以构建批处理字符串以从SharePoint列表中删除所有项目,如下所示:

  1: //create new StringBuilder
  2: StringBuilder batchString= new StringBuilder();
  3:  
  4: //add the main text to the stringbuilder
  5: batchString.Append("");
  6:  
  7: //add each item to the batch string and give it a command Delete
  8: foreach (SPListItem item in itemCollection)
  9: {
 10:    //create a new method section
 11:    batchString.Append("");
 12:    //insert the listid to know where to delete from
 13:    batchString.Append("" + Convert.ToString(item.ParentList.ID) + "");
 14:    //the item to delete
 15:    batchString.Append("" + Convert.ToString(item.ID) + "");
 16:    //set the action you would like to preform 
 17:    batchString.Append("Delete");
 18:    //close the method section
 19:    batchString.Append("");
 20: }
 21:  
 22: //close the batch section
 23: batchString.Append("");
 24:  
 25: //preform the batch
 26: SPContext.Current.Web.ProcessBatchData(batchString.ToString()); 

我能想到的唯一缺点就是你删除的所有项目都会被放入回收站。我该如何防止这种情况?

我也找到了如下解决方案:

// web is a the SPWeb object your lists belong to

// Before starting your deletion
web.Site.WebApplication.RecycleBinEnabled = false;

// When your are finished re-enable it
web.Site.WebApplication.RecycleBinEnabled = true;

Ref [Here](http://www.entwicklungsgedanken.de/2008/04/02/how-to-speed-up-the-deletion-of-large-amounts-of-list-items-within-sharepoint/)

但该解决方案的缺点是只有将来的删除才会被发送到回收站,但它会删除用户不想要的所有现有项目。有什么想法可以防止不删除现有项目吗?

非常感谢,

TQT

1 个答案:

答案 0 :(得分:0)

我认为不可能使用批量更新绕过回收站,但您可以将代码修改为类似

foreach (SPListItem item in itemCollection)
   {
      item.Delete(); //this wont go to the recycle bin
      //or if you need to send it to the recycle bin then you can use
      item.Recycle();


   }