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

[玩转系统] 修复 SharePoint Online“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”在 PowerShell 中

作者:精品下载站 日期:2024-12-14 14:45:04 浏览:15 分类:玩电脑

修复 SharePoint Online“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”在 PowerShell 中


问题:当尝试通过 PowerShell 从 SharePoint Online 列表获取列表项时,我收到此错误消息:
使用以下命令调用“ExecuteQuery”时出现异常“0”参数:“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”
在第 26 行:5
+ $Ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInitationException
+ ExcellentQualifiedErrorId : ServerException

[玩转系统] 修复 SharePoint Online“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”在 PowerShell 中

实际上,我只是想删除所有列表项:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/Sites/Projects"
$ListName="ProjectDocs"
 
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
 
#Get the web and List
$Web=$Ctx.Web
$List=$web.Lists.GetByTitle($ListName)
  
#Get all items from the library
$Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
  
#Loop through each file in the library
Foreach($Item in $ListItems)
{       
    #Delete all Items
    $List.GetItemById($Item.Id).DeleteObject()        
}
$Ctx.ExecuteQuery()
Write-host -f Green "All Items deleted!"

根本原因:这是因为 SharePoint Online 上硬编码的资源限制阈值 5000。列表视图阈值可确保用户不会在 SharePoint Online 环境中执行昂贵的操作。遗憾的是,我们无法像在本地 SharePoint 中那样更改此阈值。

解决方案:批量处理列表项以修复 PowerShell 中的 SharePoint Online 列表视图阈值问题

因此,为了缓解这个问题,我们必须批量处理项目。这是我修改后的脚本,它批量删除项目以处理 PowerShell 中的 SharePoint Online 列表视图阈值。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
$BatchSize = 500
 
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
 
    #Get the web and List
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
    Write-host "Total Number of Items Found in the List:"$List.ItemCount

    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>"

    Do {  
        #Get items from the list in batches
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
        
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
        Write-host Deleting $($ListItems.count) Items Starting from Item ID $ListItems[0].ID

        #Loop through each item and delete
        ForEach($Item in $ListItems)
        {
            $List.GetItemById($Item.Id).DeleteObject()
        } 
        $Ctx.ExecuteQuery()

    } While ($Query.ListItemCollectionPosition -ne $null)

    Write-host -f Green "All Items Deleted!"
}
Catch {
    write-host -f Red "Error Deleting List Items!" $_.Exception.Message
}

请注意,如果您的 CAML 查询检索超过 5000 行,它仍然会失败!例如,如果您尝试从可能返回超过 5000 行的大型列表中获取所有列表项(通过指定“Where”子句排除“文件夹”),您将遇到列表视图阈值问题!


$Query.ViewXml = "@
    <View Scope='RecursiveAll'> 
        <Query> 
            <Where>
                    <Eq>
                        <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
                    </Eq>
            </Where> 
        </Query>
        <RowLimit Paged='True'>$BatchSize</RowLimit>
    </View>" 

其他场景及解决方案

在包含超过 5000 个项目的大型列表上执行 cmdlet 时,您可能会遇到“禁止尝试的操作,因为它超出了列表视图阈值”。例如。添加 PnPFile、复制 PnPFile、查找 pnpfile、获取 pnpfolder、获取 pnplist、测量 pnplist、移动 pnpfolder、删除 pnplist、重命名 pnpfolder。或者,当您尝试打破继承、创建计算列、删除列表、获取项目、共享文件、重命名文件夹等时。

目前的解决方案:您必须通过移动或删除文件中的项目数低于 5000 来减少列表或文档库中的项目数。

这是另一篇文章,解决更新具有相同问题的大型列表项:SharePoint Online:使用 PowerShell 在没有列表视图的情况下从大型列表中获取所有列表项超出错误

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

取消回复欢迎 发表评论:

关灯