Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 访问SharePoint扩展属性_Powershell_Sharepoint_Sharepoint 2010_Odata_Powershell 3.0 - Fatal编程技术网

Powershell 访问SharePoint扩展属性

Powershell 访问SharePoint扩展属性,powershell,sharepoint,sharepoint-2010,odata,powershell-3.0,Powershell,Sharepoint,Sharepoint 2010,Odata,Powershell 3.0,我正在访问一个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,Sta

我正在访问一个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
在枚举提要的属性时,如何引用涉众的属性

使用此代码,不会填充
涉众.Name
属性:

(Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredentials).entry.content.properties | Foreach {
  [PsCustomObject]@{
    Id=$_.Id."#text"; 
    Title=$_.Title; 
    StakholderName=$_.Stakeholder.Name;
  }
}

是否需要为干系人填充第二个
PsCustomObject
,然后合并“主要”数据?

由于必须使用单引号字符串文字转义符号,因此查询格式不正确,例如:

$url = "http://contoso.intranet.com/_vti_bin/listdata.svc/TheList?`$select=Id,Title,Stakeholder/Name&`$expand=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
格式返回结果

不幸的是,既不是也不是 是否可用于此目的 因为根据

此特定错误阻止我们使用SharePoint REST服务,因为 由于无法指定
Accept
标题,因此无法指定结果 以
json
格式返回

话虽如此,我还是建议使用杠杆

下面是以
JSON
格式返回结果的相同示例。请注意,与原始示例相比,获取
列表项
属性变得更容易:

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

有趣的是,OData查询不需要转义
$
。上的示例似乎表明这是不必要的。
executerequestjson
函数非常有用——谢谢!鉴于
convertfromjson
生成了一个
PsCustomObject
,示例中的
Foreach
似乎没有必要。