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 ...
随机推荐
- KMP模板与讲解
读书笔记终于写完了,写一下我对KMP的理解. KMP的思想就是尽量利用已经得到的信息,来降低时间复杂度,已经得到的信息存放在next数组里.算法确实很难理解,所以很难讲解..举个例子来说吧. 设字符串 ...
- Js Carousel
http://getbootstrap.com/javascript/#carousel http://owlgraphic.com/owlcarousel/#demo https://www.mob ...
- win7系统cocos2dx 3.4 绑定自定义类到Lua
Cocos2d-x 3.0开始使用bindings-generator来生成c++类的lua绑定.bindings-generator基于tolua++,通过配置tools/tolua中的ini文件以 ...
- 通用php与mysql数据库配置文件
<?php header("content-type:text/html;charset = utf-8"); $dblink = mysql_connect("l ...
- csu 10月 月赛 J 题
Description CSU又到了一年中评奖学金的时候了……各大学霸都或多或少地拿到了各种奖学金(你们自己看着办吧). 在这里,评奖学金有个很奇怪的规矩——每个同学得到的奖学金数一定满足相邻的两个非 ...
- Codeforces Round #197 (Div. 2) : B
也是水题一个,不过稍微要细心点.... 贴代码: #include<iostream> using namespace std; long long n,m; ; int main() { ...
- MDN > Web technology for developers > HTTP
The Hypertext Transfer Protocol (HTTP) is an application-layer protocol for transmitting hypermedia ...
- angular2 学习笔记 ( Router 路由 )
参考 : https://angular.cn/docs/ts/latest/guide/router.html#!#can-activate-guard https://angular.cn/doc ...
- 断命windows上卸载node并重装
抠门儿世界500强给前端开发人员用windows windows不支持n模块没法自动升级 不记得何时安装的旧版本node连个uninstaller都找不到 绕道安装nvm path也自动加进去了丫命令 ...
- Multiplication Puzzle
题目大致意思是:一个整数序列包含N个1~100的整数(3<=N<=100),从中取出一个数并和相邻两边的整数相乘,依次进行下去直到只剩下首尾两个数为止,求最终的得到的和的最小值.两边的数不 ...