9

I'm trying to user the contains filter on a /users query, like this for example: https://graph.microsoft.com/v1.0/users?$filter=contains(displayName, 'Garth')

However, this results in a BadRequest response saying "An unknown function with name 'contains' was found. This may also be a key lookup on a navigation property, which is not allowed."

According to the OData 4.0 specs, the contains filter should be available though. Is there a way to use a contains filter on a list of users?

bstrm
  • 159
  • 1
  • 1
  • 7

3 Answers3

12

The contains function is not available for users. startsWith is available though.
e.g. https://graph.microsoft.com/v1.0/users?$filter=startswith(displayName,'Garth').

AeroX
  • 3,387
  • 2
  • 25
  • 39
Marek Rycharski
  • 1,614
  • 11
  • 10
  • And in 2022 it's still the case. Whilst not wanting to shoot the messenger, this isn't really the same thing at all. How MS thought this was acceptable is frankly baffling. – Kinetic Oct 17 '22 at 10:56
6

Additionally you can try the people API (this is only available on /beta). This supports $search AND will do fuzzy and phonetic matching. https://graph.microsoft.com/beta/me/people?$search=Garth

Dan Kershaw - MSFT
  • 5,833
  • 1
  • 14
  • 23
  • 2
    This search happens only through your contact list (or maybe through email search). But results are really strange and looks not relevant for me. It is not looking for people through out all your tenant – Roman Gusiev May 22 '17 at 18:57
  • That is correct - people is about the people you communicate with most often. Currently it does not allow search across all users in your tenant. Marek's point is valid - we don't support search for users - only the startswith filter. You could file a request for this on UserVoice - https://officespdev.uservoice.com/forums/224641-feature-requests-and-feedback/category/101632-microsoft-graph-o365-rest-apis – Dan Kershaw - MSFT May 24 '17 at 01:34
  • 1
    I'm surprised that this is not implemented yet... – C1rdec Nov 01 '21 at 21:10
  • And now the user voice has vanished. Link broken – Kinetic Oct 17 '22 at 10:56
0

$search can replace $contains for example to show all the users that their names may contain the string "sa" https://graph.microsoft.com/v1.0/users?$count=true&$search="givenName:sa"

  "value": [
        {....
           
            "givenName": "Sammy",
          ..... 
        },
        {
           .....
            "givenName": "Sabrina",
           .......
        },


.....