liuxiaoyun111 发表于 2017-5-24 11:31:08

How to delete a large number of data in SharePoint for List when refreshing data

  Preface
  Recently thequestion was asked in the newsgroups about deleting a large number of itemsfrom SharePoint (WSS) in the fastest way. I had, inone ifmyprojects,needed to remove a large number of items from SharePointand the best way Ifound were to use 'ProcessBatchData' as it avoided the API and was considerablyfaster.
  1. Delete Common List
  1.1 CAML format
  <?xml version="1.0"encoding="UTF-8"?>
<Batch>
<Method>
<SetListScope="Request">3010913d-9373-44ec-a799-0bf564cb0d66</SetList>
<SetVar Name="Cmd">DELETE</SetVar>
<SetVar Name="ID">1</SetVar>
</Method>
</Batch>
  1.2 C# source code implementation
  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("<SetListScope=\"Request\">" + CurrentList.ID +"</SetList>");
sbDelete.Append("<SetVarName=\"ID\">" + Convert.ToString(item.ID) +"</SetVar>");
sbDelete.Append("<SetVarName=\"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;
}
  2. Delete Documentlibrary list
  2.1 CAML (Collaborative Application Markup Languageformat
  <?xml version="1.0"encoding="UTF-8"?>
<Batch>
<Method ID='1' Cmd='Delete'>
<Field Name='ID'>1</Field>
<Field Name='FileRef'>http://basesmcdev/sites/tester1/myfile.bmp</Field>
</Method>
</Batch>
  2.2 C# source code implementation
  SPSecurity.RunWithElevatedPrivileges(delegate()
{
using(SPSitesite=newSPSite("http://virus/sites/intranet"))
{

using(SPWebweb=site.OpenWeb("team"))
{
site.AllowUnsafeUpdates=true;
web.AllowUnsafeUpdates=true;

SPListlist=web.Lists["documentExample"];
StringBuildersbDelete=newStringBuilder();
sbDelete.Append("<?xmlversion=\"1.0\"encoding=\"UTF-8\"?><Batch>");

foreach(SPListItemiteminlist.Items)
{
sbDelete.Append("<Method>");
sbDelete.Append("<SetListScope=\"Request\">"+list.ID+"</SetList>");
sbDelete.Append("<SetVarName=\"ID\">"+Convert.ToString(item.ID)+"</SetVar>");
sbDelete.Append("<SetVarName=\"Cmd\">Delete</SetVar>");
sbDelete.Append("<SetVarName=\"owsfileref\">"+item.GetFormattedValue("FileRef")+"</SetVar>");
sbDelete.Append("</Method>");
}

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

try
{
Console.WriteLine(web.ProcessBatchData(sbDelete.ToString()));
}
catch
{

throw;
}
site.AllowUnsafeUpdates=false;
web.AllowUnsafeUpdates=false;
}
}
});
  Reference documentation
  1.http://www.cnblogs.com/laputa-sky/archive/2008/10/21/1299867.html
  2.http://www.cnblogs.com/virusswb/archive/2009/01/21/1379275.html
  3.http://msdn.microsoft.com/zh-cn/library/ms414322(v=office.14).aspx
  4.http://msdn.microsoft.com/zh-cn/library/ms426449(v=office.14).aspx
  5.Delete Folder http://platinumdogs.me/2009/07/13/delete-a-folder-from-a-sharepoint-document-library/
页: [1]
查看完整版本: How to delete a large number of data in SharePoint for List when refreshing data