![](/img/trans.png)
[英]How do I properly perform a subquery for an ID when adding items to a list using Sharepoint CSOM?
[英]How do I retrieve all items from a Sharepoint list using CSOM if I can only retrieve N items as a time?
我正在编写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
}
从技术上讲,没有人会阻止您一次检索所有行。 如果您看到CreateAllItemsQuery
文档,则有一个重载,它将使您获得所有行
如果每个查询的行数限制为N,则必须通过循环来检索它。
看一下这篇文章: http : //blogs.msdn.com/b/kaevans/archive/2012/02/13/iterating-large-sharepoint-lists-with-powershell.aspx
我在这里复制代码:
$web = Get-SPWeb http://portal.sharepoint.com
$list = $web.Lists["LargeList"]
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.RowLimit = 2000
$caml = '<OrderBy Override="TRUE"><FieldRef Name="ID"/></OrderBy>'
$spQuery.Query = $caml
do
{
$listItems = $list.GetItems($spQuery)
$spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach($item in $listItems)
{
Write-Host $item.Title
}
}
while ($spQuery.ListItemCollectionPosition -ne $null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.