Search code examples
powershellsharepointsharepoint-2010odata

Filter a multiple-select user field using SharePoint's OData API


If a SharePoint list has a multiple-select user field (DeveloperSecondary):

enter image description here

What is the right way to filter by this field? Both of these queries produce a 404 error:

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')

If this was a single-select user field, the OData query would be:

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

Solution

  • Unfortunately $filter query option for multi-valued user field is not supported in SharePoint 2010 OData API.

    As a workaround you could consider to apply filter to the returned results as demonstrated below:

    $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')} }
    

    where

    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
    }