2017-07-27 87 views
1

我試圖從PowerShell中的SharePoint列表聯機列表中獲取項目(並在第二步中更新它們)。Powershell使用CSOM API從Sharepoint獲取列表項目

我已經嘗試了幾個在網上的例子,但無法弄清楚是什麼問題。

當運行類似於下面的示例代碼:How to get items from a sharepoint online list using PowerShell

$url ="https://company.sharepoint.com/sites/some/site/link" 
$username="[email protected]" 
$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

回答

0

不是在PowerShell中的專家,但我認爲這條線

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

嘗試輸出調用到控制檯的結果和一些道具不被初始化出於某些原因。 嘗試改變,以相同的文章:

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

和寫入項道具foreach循環安慰。

相關問題