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?的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Core Data系列文章(一)Core Data基础

    在iOS开发数据库SQLite的使用介绍了iOS中使用SQLite对数据进行持久化存储,实际上是对数据库直接进行操作,而苹果专门有一套API来间接的对数据进行持久化存储,而且主要针对用户创建的对象 - ...

  4. 谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持

    谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持 在本篇文章上一部分Order Processing的例 ...

  5. 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 ...

  6. 什么是data:image/png;base64,?一道关于Data URI Scheme的入门级CTF_Web题

    一道关于Data URI Scheme的入门级CTF_Web题 0x00 题目描述 这是偶尔遇到的某网安交流群的入群题,题目没有任何的提示,直接给了一个txt文件. 0x01 解题过程 通过给的这个文 ...

  7. 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 ...

  8. inserting a large number of records with SQLiteStatement.

    Below is a demo application I wrote that creates 100 records programmatically, inserts them using on ...

  9. 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 ...

随机推荐

  1. ASP.NET注意事项

    1.服务器上bin目录下面的dll备份的时候,第一个点号之前的名字不能是一样的,否则会报错. 2.

  2. KVC - 键值编码

    [基本概念] 1.键值编码是一个用于间接访问对象属性的机制,使用该机制不需要调用存取方法和变量实例就可访问对象属性. 2.键值编码方法在OC非正式协议(类目)NSKeyValueCoding中被声明, ...

  3. [原博客] POJ 2975 Nim 统计必胜走法个数

    题目链接题意介绍了一遍Nim取石子游戏,可以看上一篇文章详细介绍.问当前状态的必胜走法个数,也就是走到必败状态的方法数. 我们设sg为所有个数的Xor值.首先如果sg==0,它不可能有必胜走法,输出0 ...

  4. Cloud Insight!StatsD 系监控产品新宠!

    年关将至,Cloud Insight 正式版悄然上线了.没有大张旗鼓的宣传,也没有热热闹闹的庆祝,只是一群人在上线前踏踏实实的优化了两周,然后发版,就是这样一件简单的事. 然而就是这样一个低调的产品, ...

  5. 【POJ 1984】Navigation Nightmare(带权并查集)

    Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...

  6. 最好的程序员都是行动派(成功者不是那些明知赚钱之法还要推三阻四的人。成功者知道轻重缓急,善于把握今天) good

    我相信,所有程序员都需要在下面两点之间找到一个良好的平衡: 1.把自己关在一间私密的办公室里,针对你的程序与编译器展开一次亲密对话. 2.出入公众场合,与其他人公开谈论你的程序. 关于这个话题,我已经 ...

  7. [cocos2dx]计算scrollview元素的index

    scrollview的原生代码没有提供元素对齐功能 通过下面介绍的index计算方法以及scrollview自带的设置位置方法 void setContentOffsetInDuration(CCPo ...

  8. Perl脚本学习经验(三)--Perl中ftp的使用

    使用use Net::FTP;Demo:    my $Server = '192.168.1.1';    my $User = 'admin';    my $Password = 'admin' ...

  9. nyist 61 传纸条 nyist 712 探 寻 宝 藏(双线程dp问题)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=61 http://acm.nyist.net/JudgeOnline/problem.php?pid ...

  10. USACO3.35A Game(记忆化)

    刚开始理解有点误,想成每步都是最优的 ,结果卡在第六组数据上,, 无奈瞧了下别人的 发现自己理解错了,,我看的还是中文翻译.. 简单的记忆化 /* ID: shangca2 LANG: C++ TAS ...