客户端 SharePoint PowerShell - 获取列表项 - 集合尚未初始化
Client-side SharePoint PowerShell - Get list items - The collection has not been initialized
在 SharePoint 2013 上,我尝试使用客户端 SharePoint PowerShell 获取列表项。
即使对于字段 Id 或 Title,我也会遇到此错误:集合尚未初始化。
我不知道如何包含字段。我在 C# 或 JavaScript 中找到许多示例,但在客户端 powershell 中找到 none。
这是我的代码(returns 正确的项目数):
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]
或使用 $item.Title
而不是 $item["Title"]
的列值
正确的代码是:
#Import the required DLL
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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()
在行中按索引获取特定列表项时:
$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
}
在 SharePoint 2013 上,我尝试使用客户端 SharePoint PowerShell 获取列表项。 即使对于字段 Id 或 Title,我也会遇到此错误:集合尚未初始化。 我不知道如何包含字段。我在 C# 或 JavaScript 中找到许多示例,但在客户端 powershell 中找到 none。
这是我的代码(returns 正确的项目数):
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]
或使用 $item.Title
而不是 $item["Title"]
正确的代码是:
#Import the required DLL
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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()
在行中按索引获取特定列表项时:
$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
}