简体   繁体   中英

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.

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

$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

Not an expert in PowerShell, but I think that line

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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