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, in one if my projects,needed to remove a large number of items from SharePoint
and 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 (SPSite site = new SPSite("http://virus/sites/intranet"))

                {



                    using (SPWeb web = site.OpenWeb("team"))

                    {

                        site.AllowUnsafeUpdates = true;

                        web.AllowUnsafeUpdates = true;



                        SPList list = web.Lists["documentExample"];

                        StringBuilder sbDelete = new StringBuilder();

                        sbDelete.Append(\"?><Batch>");



                        foreach (SPListItem item in list.Items)

                        {

                            sbDelete.Append("<Method>");

                            sbDelete.Append("<SetList Scope=\"Request\">" + list.ID + "</SetList>");

                            sbDelete.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");

                            sbDelete.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");

                            sbDelete.Append("<SetVar Name=\"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/

上一篇:java 开发业务逻辑的思考(1)- 通知短信发送


下一篇:快速开发平台分享-UCML快速开发七种武器