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

[玩转系统] SharePoint Online:使用 PowerShell 从大型列表(> 5000 项)中获取列表项,而不会出现列表视图阈值超出错误

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

SharePoint Online:使用 PowerShell 从大型列表(> 5000 项)中获取列表项,而不会出现列表视图阈值超出错误


问题:当您尝试通过 PowerShell 从包含超过 5000 个项目的较大列表中获取项目时,您会在 SharePoint Online 中遇到超出列表视图阈值的问题!

从列表中获取列表项的典型 PowerShell CSOM 脚本导致错误:
“使用“0”参数调用“ExecuteQuery”时出现异常:“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”

[玩转系统] SharePoint Online:使用 PowerShell 从大型列表(> 5000 项)中获取列表项,而不会出现列表视图阈值超出错误

根本原因:SharePoint Online 中的项目列表视图阈值为 5000!

正如错误消息所示,我们将 5000 作为 SharePoint Online 中列表视图阈值的硬限制。任何在 SharePoint Online 中执行批量操作(例如读/写超过 5000 个项目)的尝试都会引发此问题。不幸的是,与本地 SharePoint 不同,此阈值限制无法增加!那么,让我们看看如何使用 CAML 来查询 SharePoint Online 中的大型列表。

如何使用 PowerShell 从大型列表中获取项目而不出现超出列表视图阈值的错误?

如何克服 SharePoint Online 中的列表视图阈值?有多种解决方案可以解决这个问题。这是我的:批量获取列表项!

PowerShell 从大型列表中获取列表项:

以下是如何在 SharePoint Online 列表中使用 PowerShell 获取超过 5000 条记录,而不会出现超出列表视图阈值的错误。


#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= 2000

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
        
    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #Define Query to get List Items in batch
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View Scope='RecursiveAll'><RowLimit>$BatchSize</RowLimit></View>"

    #Get List Items in Batch
    Do 
    {
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
        $ListItems.count
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
    }
    While($Query.ListItemCollectionPosition -ne $null)

}
Catch {
    write-host -f Red "Error Getting List Items:" $_.Exception.Message
} 

只需确保您的批次少于 5000,以避免 SharePoint Online 上的 5000 限制阈值问题!

使用 PnP PowerShell 从较大列表中获取列表项

PnP PowerShell 模块提供了“PageSize”开关来批量处理较大的列表:


#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName= "Projects"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all list items from list in batches
$ListItems = Get-PnPListItem -List $ListName -PageSize 500

Write-host "Total Number of List Items:" $($ListItems.Count)

#Loop through each Item
ForEach($Item in $ListItems)
{  
    Write-Host "Id :" $Item["ID"] 
    Write-Host "Title :" $Item["Title"]
}

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

取消回复欢迎 发表评论:

关灯