Powershell使用CSOM API从Sharepoint在线获取列表项

时间:2017-07-27 17:45:52

标签: list powershell sharepoint collections csom

我正试图从powershell的sharepoint在线列表中获取项目(并在第二步中更新它们)。

我在网上尝试了几个例子,但无法弄清问题是什么。

运行代码时,如下例所示:How to get items from a sharepoint online list using PowerShell

$url ="https://company.sharepoint.com/sites/some/site/link"
$username="user@domain.com"
$password="pw"
$Password = $password |ConvertTo-SecureString -AsPlainText -force

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" 

Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
    $list = $Context.Web.Lists.GetByTitle($listTitle)
    $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $items = $list.GetItems($qry)
    $Context.Load($items)
    $Context.ExecuteQuery()
    return $items 
}

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) 
$Context.Credentials = $credentials 
$context.RequestTimeOut = 5000 * 60 * 10;

$web = $context.Web
$context.load($web)
$context.ExecuteQuery()

Get-ListItems -Context $context -ListTitle "myListTitle"

我总是收到这个错误:

format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
+ CategoryInfo          : NotSpecified: (:) [format-default], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand

你知道可能出现什么问题吗?

提前致谢 Duffkess

1 个答案:

答案 0 :(得分:0)

不是PowerShell的专家,但我认为该行

Get-ListItems -Context $context -ListTitle "myListTitle"

尝试将调用的结果输出到控制台,并且由于某些原因未初始化某些道具。 尝试将其更改为与文章相同:

$items = Get-ListItems -Context $context -ListTitle "Tasks" 
foreach($item in $items)
{
   #...
}

并在foreach循环中将项目道具写入控制台。