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/
How to delete a large number of data in SharePoint for List when refreshing data?的更多相关文章
- Searching External Data in SharePoint 2010 Using Business Connectivity Services
from:http://blogs.msdn.com/b/ericwhite/archive/2010/04/28/searching-external-data-in-sharepoint-2010 ...
- Error message when you try to modify or to delete an alternate access mapping in Windows SharePoint Services 3.0: "An update conflict has occurred, and you must re-try this action"
Article ID: 939308 - View products that this article applies to. Expand all | Collapse all Symptoms ...
- Core Data系列文章(一)Core Data基础
在iOS开发数据库SQLite的使用介绍了iOS中使用SQLite对数据进行持久化存储,实际上是对数据库直接进行操作,而苹果专门有一套API来间接的对数据进行持久化存储,而且主要针对用户创建的对象 - ...
- 谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持
谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持 在本篇文章上一部分Order Processing的例 ...
- jmeter "you cannot switch bacause data cannot be converted to target Tab data,empty data to switch"报错
jmeter "you cannot switch bacause data cannot be converted to target Tab data,empty data to swi ...
- 什么是data:image/png;base64,?一道关于Data URI Scheme的入门级CTF_Web题
一道关于Data URI Scheme的入门级CTF_Web题 0x00 题目描述 这是偶尔遇到的某网安交流群的入群题,题目没有任何的提示,直接给了一个txt文件. 0x01 解题过程 通过给的这个文 ...
- shell delete with line number
If you want to delete lines 5 through 10 and 12: sed -e '5,10d;12d' file This will print the results ...
- inserting a large number of records with SQLiteStatement.
Below is a demo application I wrote that creates 100 records programmatically, inserts them using on ...
- csharp: ODP.NET,System.Data.OracleClient(.net 4.0) and System.Data.OleDb读取Oracle g 11.2.0的区别
ODP.NET: 引用: using Oracle.DataAccess; //Oracle g 11.2.0 using Oracle.DataAccess.Client; using Oracle ...
随机推荐
- Install SharePoint 2013 on Windows Server 2012 without a domain
Any setup of Team Foundation Server is not complete until you have at least tried t work with ShareP ...
- iOS开发之自定义画板
今天整好有时间, 写了一个自定义的画板! [我的github] GLPaint主要采用QuartzCore框架, 对画布上的元素进行渲染, 然后通过UIImageWriteToSavedPhotos ...
- ImageButton和Button区别
一.基础准备 Imagebutton 继承 Imageview,就是用一个图标代表了一些文字,它没Android:text属性.它由Android:src指定图标的位置 android:s ...
- [原博客] HEOI2014 行记
HEOI: 河北省信息学竞赛省队选拔赛 HEOI数据标程下载 百度盘 http://pan.baidu.com/s/1qWx7YAo 又到了一年一度的HEOI呢. 我果然还是太弱了呢. Day0 报到 ...
- IE连EXCHANGE的HTTPS时,出现错误
"There is a problem with this website's security certificate" 这种问题,一般是SSL或CA认证证书的问题. 一般从服务 ...
- 【UVA 11354】 Bond (最小瓶颈生成树、树上倍增)
[题意] n个点m条边的图 q次询问 找到一条从s到t的一条边 使所有边的最大危险系数最小 InputThere will be at most 5 cases in the input file.T ...
- 【BZOJ 1597】 [Usaco2008 Mar]土地购买 (斜率优化)
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3601 Solved: 1322 Descrip ...
- HTML Jquery;marquee标签
在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...
- 关于Json传递的日期/Date(数字)/解析
在将DateTime类型的数据Json后传到前台展示,出现如下效果 ,在客户端如何解析呢?在jquery easyui 的字段中加一个格式化的函数调用. { field: 'CreateTime' ...
- django 常用命令
django 常用命令,备忘: django-admin.py startproject test 创建一个项目名叫test的项目 python manage.py startapp app 创建一个 ...