Tuesday, January 11, 2011

Deleting Document Library Items for large list size Failed?

For novice to use this code, specify the strings variables siteURL, listName.
The maximum items that your program can delete is 25000 which can be changed.

Hey friends, If you have saved lot of time using my code, please pay me by smiling at all the people you see today. They will smile back at you. We will all be in a pool of happy smiles. If you have a big wallet, feed a hungry stomach today.  And God will bless you.

using (SPSite site = new SPSite(siteURL))
{
 using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    StringBuilder sbDelete = new StringBuilder();
                    SPList spList = web.GetList("/" + listName);
                    sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
                    string command = "<Method><SetList Scope=\"Request\">" + spList.ID +
                        "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{1}</SetVar></Method>";
                    int limit = 0;
                    foreach (SPListItem item in spList.Items)
                    {
                        limit++;
                        if (limit > 25000)
                            break;
                        sbDelete.Append(string.Format(command, item.ID.ToString(), item.File.Url));
                    }
                    sbDelete.Append("</Batch>");

                    web.ProcessBatchData(sbDelete.ToString());
                    web.Update();
                    web.AllowUnsafeUpdates = false;
                }
}

No comments: