我有一个现有的系统,我们正在清理数千个文档库并将内容移动到Azure中。我正在寻找一种编程方式来迭代列表以找到空的并删除它们。有没有人有任何样本(最好使用CSOM或powershell)来实现他们愿意分享的样本?
此时,我已经提出以下代码来完成任务,但是我收到错误"底层连接已关闭:接收时出现意外错误&# 34;尝试加载由于超时导致的列表,因为我有这么多列表。到目前为止,我的解决方案隐藏了uid,pws,site的用户秘密。
var cred = new SharePointOnlineCredentials(uid, pws);
var context = new ClientContext(site);
context.Credentials = cred;
context.RequestTimeout = Timeout.Infinite;
var lists = context.Web.Lists;
context.Load(lists);
context.ExecuteQuery();
foreach(var list in lists)
{
list.DeleteObject();
list.Update();
}
答案 0 :(得分:0)
尝试下面的代码,这里要做的就是指定要忽略的列表,已经列出了一些系统列表,确保您没有从SharePoint中删除重要内容。
下面的脚本使用SharePoint Online命令行管理程序和PnP PowerShell,在运行脚本之前先下载并安装它们:
https://www.microsoft.com/en-us/download/details.aspx?id=35588 https://github.com/SharePoint/PnP-PowerShell
cls
$url = "https://YOUR-URL-GOES-HERE.sharepoint.com"
if ($cred -eq $null)
{
$cred = Get-Credential
Connect-PnPOnline $url -Credentials $cred
}
$CSOM_context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$CSOM_credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)
$CSOM_context.Credentials = $CSOM_credentials
$lists = $CSOM_context.Web.Lists
$CSOM_context.Load($lists)
$CSOM_context.ExecuteQuery()
$ignoreList = "Form Templates", "Site Assets", "Style Library", "_catalogs/hubsite", "Translation Packages"
$lists | ? { $_.ItemCount -eq 0 -and $_.BaseTemplate -eq 101 -and $_.Title -inotin $ignoreList} | % {
Write-Host "- " $_.Title -NoNewline
try {
Remove-PnPList -Identity $_.Title -Force
Write-Host -ForegroundColor Green " [deleted] " `n
}
catch [Execption] {
Write-Host -ForegroundColor Red " [FAILURE] - " $_.Exception.Message `n
}
}
答案 1 :(得分:0)
我不认为您需要在此处进行任何超时,您可以使用以下代码删除空文档库
using Microsoft.SharePoint.Client;
using System;
using System.Linq;
using System.Security;
namespace DeleteAllEmptyDocumentLibrary
{
class Program
{
static void Main(string[] args)
{
ListItemCollection itemCollection = null;
SecureString pswd = new SecureString();
try
{
// Site Url to scan
string webUrl = "https://SiteUrl";
string userName = "UserName";
string password = "Password";
using (ClientContext context = new ClientContext(webUrl))
{
foreach (char c in password.ToCharArray())
pswd.AppendChar(c);
// Setting credential for the above site
context.Credentials = new SharePointOnlineCredentials(userName, pswd);
context.ExecuteQuery();
var lists = context.LoadQuery(context.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary));
context.ExecuteQuery();
foreach (List list in lists)
{
try
{
// Getting all items from selected list using caml query
itemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());
//Loading selected list items
context.Load(itemCollection);
context.ExecuteQuery();
// Looping each item
if (itemCollection != null && itemCollection.Count == 0)
{
list.DeleteObject();
context.ExecuteQuery();
}
}
catch (Exception ex)
{ }
}
}
}
catch (Exception ex) { }
}
}
}