简体   繁体   English

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

[英]Powershell get list items from Sharepoint online using CSOM API

I am trying to get the items (and update them in a second step) from a sharepoint online list in powershell. 我正在尝试从Powershell中的sharepoint在线列表中获取项目(并在第二步中对其进行更新)。

I have tried several examples on the web but cant figure out what the problem is. 我在网络上尝试了几个示例,但无法弄清楚问题出在哪里。

When running the code like the following example: How to get items from a sharepoint online list using PowerShell 当运行类似以下示例的代码时: 如何使用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"

I always get this error: 我总是收到此错误:

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

Do you have some idea what could be wrong? 您知道什么地方可能出问题吗?

Thanks in advance Duffkess 在此先感谢Duffkess

Not an expert in PowerShell, but I think that line 不是PowerShell的专家,但我认为这条线

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

tries to output the result of the invocation to the console and some of the props are not initialized for some reasons. 尝试将调用结果输出到控制台,并且由于某些原因,一些道具未初始化。 Try to change that to same as in article: 尝试将其更改为与文章相同:

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

and write item props to console in foreach loop. 并编写道具以在foreach循环中进行控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM