1
votes

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="[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"

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

1

1 Answers

0
votes

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.