How to delete a large number of data in SharePoint for List when refreshing data?

本文介绍了一种使用C#和特定CAML格式批量快速删除SharePoint列表或文档库中大量项目的有效方法。此方法通过避免调用API实现更快的数据处理速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Preface

Recently thequestion was asked in the newsgroups about deleting a large number of itemsfrom SharePoint (WSS) in the fastest way. I had, inone ifmyprojects,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(SPSitesite=newSPSite("http://virus/sites/intranet"))
{

using(SPWebweb=site.OpenWeb("team"))
{
site.AllowUnsafeUpdates=true;
web.AllowUnsafeUpdates=true;

SPListlist=web.Lists["documentExample"];
StringBuildersbDelete=newStringBuilder();
sbDelete.Append("<?xmlversion=\"1.0\"encoding=\"UTF-8\"?><Batch>");

foreach(SPListItemiteminlist.Items)
{
sbDelete.Append("<Method>");
sbDelete.Append("<SetListScope=\"Request\">"+list.ID+"</SetList>");
sbDelete.Append("<SetVarName=\"ID\">"+Convert.ToString(item.ID)+"</SetVar>");
sbDelete.Append("<SetVarName=\"Cmd\">Delete</SetVar>");
sbDelete.Append("<SetVarName=\"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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值