SharePoint REST-带有skiptoken的提取列表项提供HTTP 403

时间:2018-11-12 12:57:32

标签: list rest powershell sharepoint-online

我正在使用PowerShell脚本从SharePoint Online网站获取一堆列表项。我需要使用

获取所有项目

/_api/web/lists/getbytitle('MyList')/items

REST调用。
我可以称这个端点为好,并接收前100个项目。但是,当我调用响应中收到的“ skiptoken” URL时,会得到一个HTTP 403 (Forbidden)

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

在苦苦挣扎之后,我决定重新使用CSOM和CAML查询。这不像REST服务那样灵活,并且使得处理结果更加困难,但是可以做到这一点(代码功能正常,但是将其拆分是个好主意):

$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user , $password)
$context.Credentials = $credentials

$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = '...'

$web = $ctx.Web
$lists = $web.Lists
$list = $lists.GetById($listID)

$ctx.Load($lists)
$ctx.Load($list)
$ctx.ExecuteQuery()

$resultCollection = New-Object 'System.Collections.ArrayList'

do {

    $items = $list.GetItems($query)
    $ctx.Load($items)
    $ctx.ExecuteQuery()

    $items | ForEach-Object {
        $item = #process item here
        $resultCollection.Add($item) |Out-Null
    }

    $query.ListItemCollectionPosition = $items.ListItemCollectionPosition #advance cursor

} while ($query.ListItemCollectionPosition -ne $null)