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

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

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/

在使用 WSL2(Windows Subsystem for Linux 2)时,若遇到 `python -m pip list` 命令失败并提示无法刷新包列表的问题,通常与 Python 环境配置、虚拟环境路径、或 pip 本身的兼容性有关。 ### 常见原因及解决方法 #### 1. 检查 Python 和 pip 是否正确安装 确保 Python 和 pip 已正确安装并可执行。可以分别运行以下命令验证: ```bash python --version pip --version ``` 如果提示命令未找到,可能需要安装 Python 或 pip。在基于 Debian 的系统(如 Ubuntu)中,可通过以下命令安装: ```bash sudo apt update sudo apt install python3 python3-pip ``` #### 2. 使用 `python3` 替代 `python` 在某些 Linux 发行版中,默认的 `python` 命令可能未指向 Python 3,而仍指向 Python 2(如果已安装),或者根本未设置。尝试使用 `python3` 替代: ```bash python3 -m pip list ``` #### 3. 升级 pip 并修复环境路径 如果 pip 已安装但无法正常运行,可能需要升级 pip 并确保其路径正确。运行以下命令进行升级: ```bash python3 -m pip install --upgrade pip ``` 升级后,检查 pip 是否能够正常运行: ```bash python3 -m pip list ``` #### 4. 检查虚拟环境路径 如果使用了虚拟环境(如 venv),确保虚拟环境路径未损坏或移动。进入虚拟环境后运行: ```bash source venv/bin/activate pip list ``` 如果虚拟环境路径异常,建议重新创建虚拟环境: ```bash python3 -m venv venv source venv/bin/activate pip install --upgrade pip ``` #### 5. 强制重新安装 pip 如果上述方法无效,可尝试强制卸载并重新安装 pip: ```bash sudo apt remove python3-pip sudo apt autoremove sudo apt install python3-pip ``` #### 6. 处理特定包版本冲突 如果提示与特定包(如 numpy)版本有关的错误,如 `ImportError: cannot import name ‘_validate_lengths’`,则可能是由于包版本不兼容导致。尝试降级相关包: ```bash pip install numpy==1.15.0 ``` #### 7. 使用 `--ignore-installed` 参数安装包 对于某些包安装失败的情况,可以尝试忽略已安装版本并强制安装: ```bash pip install kiwisolver --ignore-installed kiwisolver ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值