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

To delete all the list items in a SharePoint list using Batch in server side Code


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

3 comments:

  1. Let me see it Munnu once. I had not worked with Client Object Model with These kind of Multiple Deletion.

    ReplyDelete
  2. I dont know the option in Client Object Model, but we can do it in Javascript Web Services.

    ReplyDelete