0

On SharePoint 2013, I am trying to get list items, with client-side SharePoint PowerShell. Even for field Id or Title, I encounter this error: The collection has not been initialized. I don't know how to include fields. I find many exemples in C# or JavaScript but none in client side powershell.

Here is my code (it returns correctly the number of items):

Function New-Context([String]$WebUrl) {
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
    $context.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
    $context
}

Function Get-List([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
    $list = $context.Web.Lists.GetByTitle($ListTitle)
    $context.Load($list)
    $context.ExecuteQuery()
    $list 
}

$context = New-Context -WebUrl "http://mysharepoint.com/sites/qp"
$list = Get-List -Context $context -ListTitle "QP-Configuration"

$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($query)
$context.Load($items)
$context.ExecuteQuery()

$items.Count
$items[0]

foreach($item in $items)
{
   $item.Id
}

$context.Dispose()
Cyril
  • 134
  • 1
  • 4
  • 15
  • 2
    Please post the full error, including line number etc – Mathias R. Jessen Jun 13 '16 at 11:50
  • Full error either you try to access an item (either $items[0] or in the foreach) `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` – Cyril Jun 14 '16 at 13:32

2 Answers2

2

While getting a specific list item by index at line:

$items[0]

or iterating through a collection of Microsoft.SharePoint.Client.ListItemCollection:

foreach($item in $items)
{
   #...
}

every property of object is assumed initialized in PowerShell but this is not the case with Microsoft.SharePoint.Client.ListItem since only a specific subset of properties could be retrieved and this why this error occurs.

To retrieve list items values I would suggest to access them via ListItem.FieldValues property:

#get list items values
$dataValues = @()
$items.GetEnumerator() | % { 
    $dataValues += $_.FieldValues 
}

Example

$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($query)
$context.Load($items)
$context.ExecuteQuery()

$dataValues = @()
$items.GetEnumerator() | % { 
    $dataValues += $_.FieldValues 
}

$dataValues.Count #determine the amount of items
$dataValues[0]  #access item by index

#iterate through list items and print a specific field value, for example FileRef 
foreach($item in $dataValues)
{
   $item.FileRef
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
0

The error was to try to get the full item like that: $items[0] or a column value using $item.Title instead of $item["Title"]

The correct code is:

#Import the required DLL
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'

Function New-Context([String]$WebUrl) {
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
    $context.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
    $context
}

Function Get-List([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
    $list = $context.Web.Lists.GetByTitle($ListTitle)
    $context.Load($list)
    $context.ExecuteQuery()
    $list 
}

$context = New-Context -WebUrl "http://mysharepointsite.com/sites/qp"
$list = Get-List -Context $context -ListTitle "QP-Configuration"

$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($query)
$context.Load($items)
$context.ExecuteQuery()

$items.Count

foreach($item in $items)
{
   $item["Title"]
}

$context.Dispose()
Cyril
  • 134
  • 1
  • 4
  • 15
  • If it has nothing to do with the function, why is it working without the function? The problematic function was Get-List – Cyril Jun 16 '16 at 18:00
  • 1
    You are right, it wasn't the function. The problem was to try to get the full item like that $items[0] or a column value using `$item.Title` instead of `$item["Title"]` – Cyril Jun 24 '16 at 16:00