Anders Hal Werner
Anders Hal Werner

Reputation: 51

Select specific fields from users and expanded manager

I want to get manager relation for all users and this can easily be done with https://graph.microsoft.com/beta/users?expand=manager.

But I get all data on all users and all data for each manager, which is way too much! I want to limit my result set to only return id and displayName for user and only id on the manager relation.

https://graph.microsoft.com/beta/users?select=id,displayName&expand=manager(select=id)

This is not working and I get this error:

Term 'manager($select=id)' is not valid in a $select or $expand expression.

Any help is much appreciated.

Upvotes: 5

Views: 3686

Answers (2)

manotheshark
manotheshark

Reputation: 4357

Here is graph API documentation that shows how to expand the entire management chain and return selected fields. Slight caveat that it works on the documentation page but it doesn't return the expected results at https://developer.microsoft.com/en-us/graph/graph-explorer.

https://graph.microsoft.com/v1.0/me?$expand=manager($levels=max;$select=id,displayName)&$select=id,displayName&$count=true

Link to graph API User List Manager:

https://learn.microsoft.com/en-us/graph/api/user-list-manager?view=graph-rest-1.0&tabs=http#code-try-8

Upvotes: 1

Shai Petel
Shai Petel

Reputation: 316

Sad to say, I was looking for this as well - it seems that this is not supported:

https://developer.microsoft.com/en-us/graph/docs/concepts/query_parameters#expand-parameter

Not all resources or relationships support using $select on expanded items.

I think this means you cant use select on manager expand.

I would take it further even and say you cannot use select on the users query itself, since there is no way to include the expanded "manager" in the select, even with all its properties. Once you set a select statement your expanded manager property will be gone.

There are two choices at this point:

  1. get the users without their manager, then create batch requests of 20 at a time to get the users managers (id only)
  2. give up on trying to be efficient, and just get all users with their managers with all their properties. This would bloat your requests but you will get away with fewer requests.

I think depending on how much data you are planning to get (number of users, properties you need) you should choose the best way forward for you.

Upvotes: 2

Related Questions