我正在访问一个SharePoint列表,它有一个相关的涉众实体--我很难访问涉众的属性。
“主”内容的属性位于xpath /feed/entry/content/properties/*上。涉众的属性位于xpath /feed/entry/link/inline/entry/content/properties/*上。
假设我在odata查询中包含了涉众的名称:
http://server/list/_vti_bin/listdata.svc/TheList?$select=Id,Title,Stakeholder/Name&$expand=Stakeholder当枚举提要的属性时,如何引用涉众的属性?
使用此代码,不填充Stakeholder.Name属性:
(Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredentials).entry.content.properties | Foreach {
[PsCustomObject]@{
Id=$_.Id."#text";
Title=$_.Title;
StakholderName=$_.Stakeholder.Name;
}
}我是否需要为涉众填充第二个PsCustomObject,然后合并“主”数据?
发布于 2015-05-12 21:36:05
该查询格式错误,因为必须使用单引号字符串文本转义$符号,例如:
$url = "http://contoso.intranet.com/_vti_bin/listdata.svc/TheList?`$select=Id,Title,Stakeholder/Name&`$expand=Stakeholder"然后可以检索Stakeholder值,如下所示:
$StakeholderValue = $data.link | where { $_.title -eq "Stakeholder" } | select -Property @{name="Name";expression={$($_.inline.entry.content.properties.Name)}}修改的示例
$url = "http://contoso.intranet.com/_vti_bin/listdata.svc/TheList?`$select=Id,Title,Stakeholder/Name&`$expand=Stakeholder"
$data = Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredentials -ContentType "application/json;odata=verbose"
$data | Foreach {
[PsCustomObject]@{
Id = $_.content.properties.Id."#text";
Title = $_.content.properties.Title;
Stakeholder = $_.link | where { $_.title -eq "Stakeholder" } | select -Property @{name="Name";expression={$($_.inline.entry.content.properties.Name)}}
}
}另外,我建议考虑另一种办法。默认情况下,SharePoint 2010 REST服务以xml格式返回结果。这样做的目的是以json格式返回结果。
不幸的是,调用-RestMethod方法和调用-WebRequest都不能使用而不是,因为它们都包含PowerShell 3.0中的一个bug。 这个特定的错误阻止我们使用SharePoint REST服务,因为无法指定
Accept头,因此不能以json格式返回结果。
话虽如此,我还是建议利用WebClient类。
下面演示了以JSON格式返回结果的相同示例。请注意,与原始示例相比,获取List Item属性变得更容易:
Function Execute-RequestJson()
{
Param(
[Parameter(Mandatory=$True)]
[string]$Url,
[Parameter(Mandatory=$False)]
[System.Net.ICredentials]$Credentials,
[Parameter(Mandatory=$False)]
[bool]$UseDefaultCredentials = $True,
[Parameter(Mandatory=$False)]
[Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get
)
$client = New-Object System.Net.WebClient
if($Credentials) {
$client.Credentials = $Credentials
}
elseif($UseDefaultCredentials){
$client.Credentials = [System.Net.CredentialCache]::DefaultCredentials
}
$client.Headers.Add("Content-Type", "application/json;odata=verbose")
$client.Headers.Add("Accept", "application/json;odata=verbose")
$data = $client.DownloadString($Url)
$client.Dispose()
return $data | ConvertFrom-Json
}
$url = "http://contoso.intranet.dev/_vti_bin/listdata.svc/TheList?`$select=Id,Title,Stakeholder/Name&`$expand=Stakeholder"
$data = Execute-RequestJson -Url $url -UseDefaultCredentials $true
$data.d.results | Foreach {
[PsCustomObject]@{
Id = $_.Id;
Title = $_.Title;
Stakeholder = $_.Stakeholder.Name
}
}https://stackoverflow.com/questions/30191251
复制相似问题