在O365中删除空文档库

Jim*_*ley 11 sharepoint-online

我有一个现有的系统,我们正在清理数千个文档库,并将内容移动到Azure中.我正在寻找一种编程方式迭代列表以找到空的并删除它们.有没有人有任何样本(最好使用CSOM或powershell)来实现他们愿意分享的样本?

此时,我已经提出以下代码来完成任务,但是我收到错误"底层连接已关闭:接收时发生意外错误"试图加载列表因为超时因为我有这么多名单.到目前为止,我的解决方案隐藏了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();
}
Run Code Online (Sandbox Code Playgroud)

小智 0

尝试下面的代码,您在这里要做的就是指定要忽略的列表,一些系统列表已经列出,确保您没有从 SharePoint 中删除重要内容。

下面的脚本使用 SharePoint Online Management Shell 和 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  
    }
}
Run Code Online (Sandbox Code Playgroud)