Client-side SharePoint PowerShell - Get list items - The collection has not been initialized

与世无争的帅哥 提交于 2019-12-23 05:14:25

问题


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()

回答1:


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
}



回答2:


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()


来源:https://stackoverflow.com/questions/37788615/client-side-sharepoint-powershell-get-list-items-the-collection-has-not-been

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!