当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 修复 SharePoint Online 中的“您必须先删除此文件夹中的所有项目,然后才能删除该文件夹”错误

作者:精品下载站 日期:2024-12-14 21:10:24 浏览:16 分类:玩电脑

修复 SharePoint Online 中的“您必须先删除此文件夹中的所有项目,然后才能删除该文件夹”错误


问题:尝试删除包含子文件夹和文件的文件夹时,我在 SharePoint Online 或 OneDrive 中收到错误消息“您必须先删除此文件夹中的所有项目,然后才能删除该文件夹” 。

[玩转系统] 修复 SharePoint Online 中的“您必须先删除此文件夹中的所有项目,然后才能删除该文件夹”错误

您还可能会看到“请求已被收到的事件取消。如果尝试删除非空文件夹,有时可能会出现“它处于暂停状态”错误。

[玩转系统] 修复 SharePoint Online 中的“您必须先删除此文件夹中的所有项目,然后才能删除该文件夹”错误

根本原因:

通常,我们在删除文件夹之前不需要清空该文件夹。但是,如果该网站处于保存保留策略或电子数据展示保留状态,我们可能会收到此错误消息。当网站处于暂停状态时,可以删除文档,但我们不能直接删除包含文件和子文件夹的文件夹。这就是保留保留防止数据丢失的方式。

“您必须先删除此文件夹中的所有项目才能删除该文件夹”错误的解决方案:

您可以以全局管理员身份从 Office 365 安全与合规中心禁用网站的保留策略或电子数据展示保留(并等待一段时间才能生效!),或者如错误消息所示,删除文件夹内的所有文件和子文件夹第一的!从每个文件夹中删除文件可能非常耗时,因此您可以使用“在文件资源管理器中查看”来删除包含文件的文件夹或 PowerShell,以使其更容易。

SharePoint Online:使用 PowerShell 删除包含文件的文件夹

此 PowerShell 脚本使用 Remove-PnPListItem cmdlet 删除 SharePoint Online 中给定文件夹的所有文件和子文件夹。


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName ="Branding"
$FolderServerRelativeURL = "/sites/Marketing/Branding/2019"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
      
    #Get All Items from Folder in Batch
    $ListItems = Get-PnPListItem -List $ListName -FolderServerRelativeUrl $FolderServerRelativeURL -PageSize 2000 | Sort-Object ID -Descending
  
    #Powershell to delete all files from a folder
    ForEach ($Item in $ListItems)
    {
        Remove-PnPListItem -List $ListName -Identity $Item.Id -Recycle -Force
        Write-host "Removed File:"$Item.FieldValues.FileRef
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

更多信息请参阅:如何使用 PowerShell 清空 SharePoint Online 中的文件夹?

如果您有较大的库,即使您使用了“PageSize”,带有FolderServerRelativeURL 参数的Get-PnPListItem cmdlet 也可能会失败。因此,以下是用于删除 SharePoint Online 中给定文件夹中的所有内容的 PowerShell 脚本:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/mumbai"
$ListName ="Work"
$FolderServerRelativeURL = "/sites/mumbai/w/Arun Tiwari/New folder/*"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -UseWebLogin
    $List = Get-PnPList $ListName
       
    #Get All Items from Folder in Batch
    $global:counter = 0;
    $ListItems = Get-PnPListItem -List $ListName -PageSize 2000 -Fields ID,FileDirRef,FileRef -ScriptBlock `
        { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
                "Getting Items from Folder '$FolderServerRelativeURL'" -Status "Getting Items $global:Counter of $($List.ItemCount)";}

    #Get List Items from the folder and Sort List Items based on Server Relative URL - Descending
    $ItemsFromFolder = $ListItems | Where {$_.FieldValues["FileDirRef"] -like $FolderServerRelativeURL } | Select-Object @{Name="ServerRelativeURL";Expression={$_.FieldValues.FileRef}}, ID | Sort-Object -Descending -Property ServerRelativeURL
    Write-host "Total Number of Items Found in the Folder:"$ItemsFromFolder.count
         
    #Delete List Items in Batch
    $Batch = New-PnPBatch
    ForEach ($Item in $ItemsFromFolder)
    {
        Remove-PnPListItem -List $ListName -Identity $Item.ID -Recycle -Batch $Batch
        Write-host "Item Queued for deletion:"$Item.ServerRelativeURL
    }
    Invoke-PnPBatch -Batch $Batch -Details

}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

当您签出文件时可能会发生此错误!将它们全部签入以修复:SharePoint Online:PowerShell 批量签入所有文档

总之,错误消息“您必须先删除此文件夹中的所有项目,然后才能删除该文件夹”可以通过简单地删除或移动文件夹中的项目来解决。通过执行本文中概述的步骤,您可以轻松解决此错误并根据需要删除该文件夹。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯