【问题标题】:RowLimit not working in SharePoint Online CAML queryRowLimit 在 SharePoint Online CAML 查询中不起作用
【发布时间】:2020-01-23 15:15:57
【问题描述】:

由于某种原因,RowLimit 在 SharePoint Online 中不起作用。我想知道是否支持 RowLimit 功能。如果是这样,下面的查询有什么问题?

<View><Query>
<Where><Eq><FieldRef Name='SiteStatus'/><Value Type='Text'>Completed</Value></Eq></Where>
<OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy></Query>
<ViewFields><FieldRef Name='Title'/></ViewFields>
<RowLimit>2</RowLimit>
</View>

查询模式:PowerShell PnP

【问题讨论】:

    标签: powershell sharepoint sharepoint-online caml


    【解决方案1】:

    在 CSOM PowerShell 中使用 CAML 查询进行测试,RowLimit 选项按预期工作:

    #Load SharePoint CSOM Assemblies
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
    #Set parameter values
    $SiteURL="https://tenant.sharepoint.com/sites/sitename/"
    $ListName="listname"
    
    #Get Credentials to connect
    $Cred= Get-Credential
    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
    #Get the List
    $List = $Ctx.Web.lists.GetByTitle($ListName)
    
    #Define the CAML Query
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "@
           <View>  
                <Query> 
                   <Where><Eq><FieldRef Name='SiteStatus' /><Value Type='Text'>Completed</Value></Eq></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy> 
                </Query> 
                 <ViewFields><FieldRef Name='Title' /></ViewFields> 
                <RowLimit>2</RowLimit> 
          </View>"
    
    #Get All List Items matching the query
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
    
    Write-host "Total Number of Items:"$ListItems.count
    
    #Loop through each List Item
    ForEach($Item in $ListItems)
    {
        Write-host $Item.id
        Write-host $Item["Title"]
    }
    

    这是测试列表数据:

    执行 PowerShell 时,会返回 SiteStatus 等于 Completed 的最后两项:

    【讨论】:

    • 谢谢杰瑞。我想知道 PnP 是否有任何限制,因为这就是我正在使用的。我现在将通过您的代码。如果它仍然不起作用,我将在上面的问题中发布我的代码。
    • 我正在使用 SharePoint CSOM PowerShell 进行测试,将使用 PnP PowerShell 进行测试并为您提供更新。
    • 确实 PnP PowerShell 不接受 CAML 中的 RowLimit 参数,我可以重现。并且在 GitHub 中发布了相同的问题:github.com/SharePoint/PnP-PowerShell/issues/879 相反,请在 PnP PowerShell 中限制这样的行数:Get-PnPListItem -List 'yourlist' -Query 'the CAML' -PageSize 4 |选择 -first 2
    【解决方案2】:

    如果在超过 5000 的库上执行,则行限制不起作用。

    $Query.ViewXml = "@
           <View Scope='RecursiveAll'>  
                <Query> 
                   <Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>A2.xlsx</Value></Eq></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy> 
                </Query> 
                 <ViewFields><FieldRef Name='Title' /></ViewFields> 
                <RowLimit>4999</RowLimit> 
          </View>"
    

    即使查询只有 1 个结果。 它仍然以阈值错误结束(与get-pnplistitem相同)

    【讨论】:

      【解决方案3】:

      如果列表/库的元素超过 5000 个,您可以为在“位置”条件下使用的列创建索引。这会解决你的问题。

      对于您的示例,您应该为“SiteStatus”列创建索引。 为了获得更好的性能结果,最好也为“ID”列创建索引,因为它已在“OrderBy”条件下使用。

      <View><Query>
      <Where><Eq><FieldRef Name='SiteStatus'/><Value Type='Text'>Completed</Value></Eq></Where>
      <OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy></Query>
      <ViewFields><FieldRef Name='Title'/></ViewFields>
      <RowLimit>2</RowLimit>
      </View>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多