gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:23:09 25 4
gpt4 key购买 nike

我正在编写一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com