powershell - 如果一次只能检索 N 个项目,如何使用 CSOM 从 Sharepoint 列表中检索所有项目?

标签 powershell sharepoint csom

我正在编写一个 Powershell 脚本来检索列表中的所有项目。我的上级告诉我一次只能拉取 200 行,所以我考虑到这一点编写了以下代码:

function getLookupValues($_ctx, $_listName, $_colToMatch)
{
    $lookupList = $_ctx.Web.Lists.GetByTitle($_listName)
    $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(200, 'ID', $_colToMatch)
    $vals = $lookupList.getItems($query)
    $_ctx.Load($lookupList)
    $_ctx.Load($vals)
    $_ctx.ExecuteQuery()

    return $vals
}

我正在测试的列表中有 200 多个项目。当我运行这段代码时,我只检索前 200 个项目。我想这是预期的,但我认为由于查询称为“所有项目”查询,它可能知道重复查询 200 个项目,直到它到达列表的末尾。然而,正如我通过测试发现的那样,情况并非如此。

如果每个查询限制为 N 行,那么检索列表中每个项目的正确方法是什么?我是否需要执行某种循环来重复查询并将结果转储到一个保持数组中,直到检索到所有项目为止?

最佳答案

Madhur 的回答大部分是正确的,但我需要一些可以与 Sharepoint 的客户端库一起使用的东西。以下是我如何调整代码以获得预期结果:

$mQueryRowLimit = 200
function getAllListItems($_ctx, $_listName, $_rowLimit = $mQueryRowLimit)
{
    # Load the up list
    $lookupList = $_ctx.Web.Lists.GetByTitle($_listName)
    $_ctx.Load($lookupList)

    # Prepare the query
    $query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $query.ViewXml = "<View>
        <RowLimit>$_rowLimit</RowLimit>
    </View>"

    # An array to hold all of the ListItems
    $items = @()

    # Get Items from the List until we reach the end
    do
    {
        $listItems = $lookupList.getItems($query)
        $_ctx.Load($listItems)
        $_ctx.ExecuteQuery()
        $query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition

        foreach($item in $listItems)
        {
            Try
            {
                # Add each item
                $items += $item
            }
            Catch [System.Exception]
            {
                # This shouldn't happen, but just in case
                Write-Host $_.Exception.Message
            }
        }
    }
    While($query.ListItemCollectionPosition -ne $null)

    return $items
}

关于powershell - 如果一次只能检索 N 个项目,如何使用 CSOM 从 Sharepoint 列表中检索所有项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25231415/

相关文章:

sql - 是否可以使用 "Invoke-SqlCmd"作为 Visual Studio Online 构建步骤的一部分?

windows - 如何批量重命名包含数字和修改日期的文件?

web-services - Sharepoint Web 服务教程

javascript - SharePoint 2013 - 获取应用程序中JS中所有用户的列表

javascript - 使用客户端对象模型 (CSOM) 获取 Sharepoint 选定列表项

PowerShell 函数从函数加载

sharepoint - 插入链接/图片 "From SharePoint"在功能区中变灰

csom - 使用 Project Online 和 CSOM 进行身份验证时出现错误 401

javascript - Sharepoint 2013 添加查找值到另一个列表 Javascript 列表

arrays - powershell 截断仓库名称