Wednesday, December 1, 2010

Deleting all library items in a document library.

  1. private static void DeleteAllItemsUsingBatch()  
  2.         {  
  3.             using (SPSite site = new SPSite("http://mysharepointserver"))  
  4.             {  
  5.                 SPWeb web = site.OpenWeb("/");  
  6.                 SPList list = web.Lists["Documents"];  
  7.                 StringBuilder sb = new StringBuilder();  
  8.                 sb.Append("<batch>");  
  9.                 string batchCommand = "<method><setlist scope=\"Request\">" + list.ID + "</setlist><setvar name=\"ID\">{0}</setvar><setvar name=\"Cmd\">DELETE</setvar><setvar name=\"owsfileref\">{1}</setvar></method>";  
  10.                 foreach (SPListItem item in list.Items)  
  11.                 {  
  12.                     sb.AppendFormat(batchCommand, item.ID.ToString(), item.File.ServerRelativeUrl);  
  13.                 }  
  14.                 sb.Append("</batch>");  
  15.   
  16.                 web.AllowUnsafeUpdates = true;  
  17.                 site.RootWeb.ProcessBatchData(sb.ToString());  
  18.                 web.AllowUnsafeUpdates = false;  
  19.                 web.Close();  
  20.             }  
  21.         } 

No comments:

Post a Comment