使用 SharePoint 的 OData API 筛选多个 select 用户字段
Filter a multiple-select user field using SharePoint's OData API
如果 SharePoint 列表有多个 select 用户字段 (DeveloperSecondary
):
按此字段筛选的正确方法是什么?这两个查询都会产生 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')
如果这是一个 select 用户字段,OData 查询将是:
http://server/_vti_bin/listdata.svc/list?$filter=(SingleSelectUserField/UserName eq 'foo')
遗憾的是 $filter
多值用户字段 的查询选项 不支持 SharePoint 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
}
如果 SharePoint 列表有多个 select 用户字段 (DeveloperSecondary
):
按此字段筛选的正确方法是什么?这两个查询都会产生 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')
如果这是一个 select 用户字段,OData 查询将是:
http://server/_vti_bin/listdata.svc/list?$filter=(SingleSelectUserField/UserName eq 'foo')
遗憾的是 $filter
多值用户字段 的查询选项 不支持 SharePoint 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
}