【问题标题】:Client-side SharePoint PowerShell - Get list items - The collection has not been initialized客户端 SharePoint PowerShell - 获取列表项 - 集合尚未初始化
【发布时间】:2016-10-13 19:19:04
【问题描述】:

在 SharePoint 2013 上,我正在尝试使用客户端 SharePoint PowerShell 获取列表项。 即使对于字段 Id 或 Title,我也会遇到此错误:集合尚未初始化。 我不知道如何包含字段。我在 C# 或 JavaScript 中找到了许多示例,但在客户端 powershell 中没有。

这是我的代码(它正确返回了项目数):

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

【问题讨论】:

  • 请发布完整的错误,包括行号等
  • 您尝试访问某个项目($items[0] 或在 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

标签: powershell sharepoint sharepoint-2013 sharepoint-clientobject


【解决方案1】:

错误是尝试获取完整的项目:$items[0] 或使用 $item.Title 而不是 $item["Title"] 的列值

正确的代码是:

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

【讨论】:

  • 如果和函数无关,为什么没有函数也能工作?有问题的函数是 Get-List
  • 你是对的,这不是函数。问题是尝试使用 $item.Title 而不是 $item["Title"] 来获取像 $items[0] 这样的完整项目或列值
【解决方案2】:

通过索引获取特定列表项时:

$items[0]

或遍历Microsoft.SharePoint.Client.ListItemCollection的集合:

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

假设对象的每个属性都在 PowerShell 中进行了初始化,但 Microsoft.SharePoint.Client.ListItem 并非如此,因为只能检索特定的属性子集,这就是发生此错误的原因。

要检索列表项值,我建议通过ListItem.FieldValues 属性访问它们:

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

示例

$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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    相关资源
    最近更新 更多