How to delete a large number of data in SharePoint for List when refreshing data?
2016-03-29 11:08
591 查看
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/
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/
相关文章推荐
- Androidstudio点9图报错
- FirstOrDefault()的重载方法
- bzoj 4448 [Scoi2015]情报传递(主席树,LCA)
- Html5之高级-1 HTML5表单元素(表单简介、input类型、新表单元素)
- codis-server 集群安装及部署
- 怎么把PDF中的文字提取出来
- Java 回顾笔记_集合框架-泛型高级应用
- 【BZOJ4034】[HAOI2015]T2【树链剖分】【线段树】
- Thinkpad E500在UEFI+GTP分区方案U盘安装Windows Server 2008 R2
- java基础知识汇总4
- linux下内存
- view.bringtofront
- javaScript——&amp;
- iOS开发笔记--UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
- 使用IIS实现域名跳转
- C++继承详解
- Xcode技巧:项目导入外部文件后编译运行出现Undefined symbols for architecture x86_64错误
- 283. Move Zeroes
- 页面跳转
- SVN笔记(1)