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("<?xml version=\"1.0\" encoding=\"UTF-8\"?><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/





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5332175.html,如需转载请自行联系原作者 

相关文章
|
19天前
|
数据采集 存储 数据可视化
SharePoint List
【6月更文挑战第10天】
6 1
|
26天前
|
BI
Power BI获取SharePoint List列表后,如何展开List/Table中的字段,以及使用逗号拼接为一个字符串
在Power BI中,从SharePoint List获取数据时遇到Table和List混合的数据源,直接展开会导致“笛卡尔积”效应,生成过多行。目标是保持行数不变,将Table中的字段与List值用逗号分隔显示在同一行。解决方法包括:1) 添加新列,从Table中提取List的Column2值;2) 使用Text.Combine函数合并List中的值。具体操作步骤包括选择列并自定义新列,然后展开List并以逗号分隔。通过这些步骤,可以将Table转换为所需的字符串格式。完整的Power BI Query代码展示了这一过程。参考链接提供了更多详情。
|
7月前
|
JSON 小程序 API
小程序踩坑-Setting data field "list" to undefined is invalid.
小程序踩坑-Setting data field "list" to undefined is invalid.
185 0
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
101 0
SAP Fiori Elements - fixed value help data request and how drop down list entry is rendered
SAP Fiori Elements - fixed value help data request and how drop down list entry is rendered
108 0
SAP Fiori Elements - fixed value help data request and how drop down list entry is rendered