Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Friday, December 02, 2011

Deleting all library items in a document library using SpWeb and Batch


private static void DeleteAllItemsUsingBatch()  
        {  
            using (SPSite site = new SPSite("http://mysharepointserver"))  
            {  
                SPWeb web = site.OpenWeb("/");  
                SPList list = web.Lists["Documents"];  
                StringBuilder sb = new StringBuilder();  
                sb.Append("<batch>");  
                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>";  
                foreach (SPListItem item in list.Items)  
                {  
                    sb.AppendFormat(batchCommand, item.ID.ToString(), item.File.ServerRelativeUrl);  
                }  
                sb.Append("</batch>");  
  
                web.AllowUnsafeUpdates = true;  
                site.RootWeb.ProcessBatchData(sb.ToString());  
                web.AllowUnsafeUpdates = false;  
                web.Close();  
            }  
        } 

No comments:

Post a Comment