使用SharePoint的OData API过滤多选用户字段

时间:2015-07-10 18:21:42

标签: powershell sharepoint sharepoint-2010 odata

如果SharePoint列表具有多选用户字段(DeveloperSecondary):

enter image description here

按此字段过滤的正确方法是什么?这两个查询都会产生404错误:

http://server/_vti_bin/listdata.svc/list?$filter=(DeveloperSecondary/Results/UserName eq 'foo')

http://server/_vti_bin/listdata.svc/list?$filter=(DeveloperSecondary/UserName eq 'foo')

如果这是单选用户字段,则OData查询将为:

http://server/_vti_bin/listdata.svc/list?$filter=(SingleSelectUserField/UserName eq 'foo')

1 个答案:

答案 0 :(得分:1)

很遗憾,多值用户字段$filter查询选项在 2010 OData API中

>

作为一种解决方法,您可以考虑将过滤器应用于返回的结果,如下所示:

$requestUrl = "http://contoso.intranet.dev/_vti_bin/listdata.svc/TheList?`$expand=DeveloperSecondary"
$data = Execute-RequestJson -Url $requestUrl -UseDefaultCredentials $true
#filter items
$data.d.results | % { $_.DeveloperSecondary.results | Where-Object {($_.Name -eq 'John Doe')} }

,其中

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
}