Graph API: How to use $filter in list user?

Wang, Wei 41 Reputation points
2022-10-27T18:56:58.993+00:00

Greetings! I just got started learning about Graph API, writing some Node.js code to test things out.

I have registered a developer account that gave me a sand box to play with. I got my script to list users with this simple query:
https://graph.microsoft.com/v1.0/users

That gives me the list of all users in my organization. So far so good.

Following the documentation,
https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http
I tried to use the filter to find particular users, with:
https://graph.microsoft.com/v1.0/users?%24filter%3DgivenName%20eq%20'Wang'
I was hoping to get a list of one user (myself) in return.

But I get the list of all users instead.

Tried a few other filters, like $filter=startsWith(mail,'ww'), still the same results.

What am I doing wrong? Thanks for any advice.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,447 questions
0 comments No comments
{count} votes

Accepted answer
  1. CarlZhao-MSFT 40,311 Reputation points
    2022-10-28T03:24:50.34+00:00

    Hi @Wang, Wei

    Your URL doesn't seem to be decoded, and the encoded part isn't actually recognized. The decoded API endpoints are:

    https://graph.microsoft.com/v1.0/users?$filter=givenName eq 'Wang'  
    

    By the way, make sure you specify the value of First name as Wang when you create the user, otherwise the endpoint will return an empty array.

    254954-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bhanu Kiran 3,526 Reputation points
    2022-10-27T19:41:40.43+00:00

    Hello @Wang, Wei ,

    To use $filter query parameter for List users using 'givenName', use the following endpoint:

    GET https://graph.microsoft.com/v1.0/users?$filter=givenName eq 'name'  
    

    254911-listusers.png

    Please check and validate the same.

    Refer to this document for more information on $filter query parameter

    Hope this helps,
    If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.

    0 comments No comments

  2. Wang, Wei 41 Reputation points
    2022-10-30T16:50:15.903+00:00

    Thanks for both answers. I guess I didn't experiment enough.

    From the documentations:
    https://learn.microsoft.com/en-us/graph/onedrive-addressing-driveitems
    https://learn.microsoft.com/en-us/graph/query-parameters#encoding-query-parameters
    It wasn't quite clear to me that only the <filter-value> part of the "$filter=<filter-value>" is supposed to be encoded.

    Once I got that part right, it started working for me.

    Thanks again!

    0 comments No comments