访问 SharePoint 扩展属性

Access SharePoint expanded properties

我正在访问一个 SharePoint 列表,该列表具有关联的利益相关者实体 -- 我很难访问利益相关者的属性。

'primary' 内容的属性位于 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,然后合并 'primary' 数据?

查询格式错误,因为 $ 符号必须使用单引号字符串文字进行转义,例如:

$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 服务 returns 结果采用 xml 格式。这个想法是 return 结果变成 json 格式。

Unfortunately neither Invoke-RestMethod nor Invoke-WebRequest could not be utilized for that purpose since both of them contain a bug in PowerShell 3.0 according to Microsoft Connect.

This particular bug prevents us to consume SharePoint REST services since since Accept header could not be specified and therefore results could not be returned in json format

话虽如此,我还是建议利用 WebClient Class

下面演示了 returns 生成 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
    }
}