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

[玩转系统] 修复“Get-PnPListItem:尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”错误

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

修复“Get-PnPListItem:尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”错误


问题:尝试使用带有查询参数的 Get-PnPListItem cmdlet 从 SharePoint Online 列表中获取列表项时,我收到一条错误消息 “Get- PnPListItem:尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”

[玩转系统] 修复“Get-PnPListItem:尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”错误

根本原因:

要求是获取 90 天前创建的所有列表项。因此,我尝试通过应用查询过滤器来获取列表项。正如错误消息所述,此错误是由查询非索引列时超过 5000 项的大型数据引起的!以下是导致上述错误消息的 PowerShell 脚本:


#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/Projects"
$ListName = "Meetings"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Define Query to Filter items created 90 days ago
$Query= "<View>
            <Query>
                <Where>
                    <Lt>
                        <FieldRef Name='Created' Type='DateTime'/>
                        <Value Type='DateTime' IncludeTimeValue='TRUE'>
                            <Today OffsetDays='-90'/>
                        </Value>
                    </Lt>
                </Where>
            </Query>
        </View>"

#Get All Items from the List in batches 
$ListItems = Get-PnPListItem -List $ListName -PageSize 500 -Query $Query
Write-host "Total Number of Items Found:"$ListItems.count

#Get Each Item's Created Date
$ListItems | ForEach-Object { Write-host ("List Item:{0} was Created on {1}" -f $_["FileLeafRef"],$_["Created"]) }

解决方案:

当您使用“Query”参数从大型列表中获取列表项时,使用 PageSize 开关似乎没有帮助。因此,我首先获取所有列表项,然后使用“where”子句应用过滤器。


$SiteURL= "https://crescent.sharepoint.com/sites/Projects"
$ListName = "Meetings"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get All Items from the List in batches 
$ListItems = Get-PnPListItem -List $ListName -PageSize 500
Write-host "Total Number of Items Found:"$ListItems.count

#Filter List Items
$TimeStamp = (Get-Date).AddDays(-90)
$FilteredItems  = $ListItems | Where {$_["Created"] -Lt $TimeStamp}

#Get Each Item's Created Date
$FilteredItems | ForEach-Object { Write-host ("List Item:{0} was Created on {1}" -f $_["FileLeafRef"],$_["Created"]) }

此问题已在 PnP GitHub 中提出,并且仍然处于开放状态:https://github.com/MicrosoftDocs/office-docs-powershell/issues/3457

将索引列添加到列表和查询

您还可以通过将“索引列”添加到列表(如何使用 PowerShell 在 SharePoint Online 中添加索引列?)并将索引列添加到查询来绕过该问题。例如,默认情况下会对 ID 列建立索引,以下是如何使用 CAML 查询使用 属性:


#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/pmo"
$ListName = "projects"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
  
#Define Query to get list items
$Query= "<View Scope='RecursiveAll'>
        <Query>
               <OrderBy> <FieldRef Name='ID' Ascending='TRUE' /></OrderBy> 
        </Query>
        <RowLimit>1000</RowLimit>
        </View>"
 
#Get All Items from the List in batches 
$ListItems = Get-PnPListItem -List $ListName -Query $Query
Write-host "Total Number of Items Found:"$ListItems.count

您还可以使用 Get-PnPListItem cmdlet 的“PageSize”参数来代替 RowLimit 属性:


  
#Define Query to get list items
$Query= "<View Scope='RecursiveAll'>
        <Query>
               <OrderBy> <FieldRef Name='ID' Ascending='TRUE' /></OrderBy> 
        </Query>      
        </View>"

#Get All Items from the List in batches 
$ListItems = Get-PnPListItem -List $ListName -Query $Query -PageSize 500

最后:当 SharePoint Online 或 OneDrive 中的列表或文档库在各种情况下超过 5000 个项目时,您可能会收到“禁止尝试操作,因为它超出了列表视图阈值”错误,例如:

  • 使用 CSOM 脚本或 PnP PowerShell cmdlet,例如 Add-PnPFile、Copy-PnPFile、Find-PnPFile、Get-PnPFolder、Get-PnPFile、Get-PnPListItem、executequery 等。
  • 尝试破坏权限继承、尝试重命名文件夹等。
  • 在 CSOM PowerShell 脚本中使用 CAML 查询
  • 尝试删除列表或网站时
  • 尝试使用 Power Apps 等获取列表项。

您必须减少列表或库中的项目数量才能解决此问题。

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

取消回复欢迎 发表评论:

关灯