Select specific fields from users and expanded manager - microsoft-graph-api

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.

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:
get the users without their manager, then create batch requests of 20 at a time to get the users managers (id only)
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.

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

Related

How do we use select inside an expanded collection on Microsoft Graph?

Following query in MS Graph Explorer displays signed-in user's profile and its manager Diego Siciliani
https://graph.microsoft.com/v1.0/me?$expand=manager
But you may notice in the response it is returning tons of attributes of manager object, as well. Some of these attributes are objects (such as assignedPlans) themselves and have their own attributes, as well.
Question: How can we filter the above query so it returns user profile (that it is already doing) along with ONLY the following attirbute value the user's manager: "displayName": "Diego Siciliani"
Remark: Following query returns the error shown below: https://graph.microsoft.com/v1.0/me?$expand=manager($select=displayName)
Invalid $select properties
Please use the below query to get user and his manager details
https://graph.microsoft.com/v1.0/users/userid?$expand=manager($levels=max;$select=id,displayName)&$select=id,displayName&$count=true
ConsistencyLevel eventual
Update https://graph.microsoft.com/v1.0/me?$expand=manager($levels=max;$select=displayName)&$count=true
It is known issue from Microsoft that nested $select combined with $expand doesn't work (i.e. $expand=some_path($select=some_field) does not work with Microsoft Graph API).
See: https://learn.microsoft.com/en-us/graph/known-issues#query-parameter-limitations
$expand:
No support for nextLink
No support for more than 1 level of expand
No support with extra parameters ($filter, $select)
I wish they would implement it because right now we either have to pull a lot of extra data (e.g. for managers), or we have to make a lot of requests per user to retrieve just the field we want.
There's a solution that deal with batch requesting but it requires a json solution: https://learn.microsoft.com/en-us/graph/json-batching?view=graph-rest-1.0

How to Filter Odata Query that is expanded by multiple levels?

I am currently trying to make an Odata call where I filter by a "multi-level" expanded property, however, none of the documentation or information I can find on this covers exactly what I am trying to accomplish. Does anyone know if this can be done, and how?
This is for letting our users be able to filter their Tests by the name of the Room the test was taken in. We have been able to filter by a single level of expanding, letting users filter their Tests by the name of the location, but we just can't seem to get the syntax right with the second level.
For context, our Test table is linked to a location table that is linked to a room table.
We are able to see the expanded information without a filter by calling:
.../api/Tests$expand=Location($select=Id,LocationName;$expand=Room($select=Id,RoomName))
returns:
...{"Id":19955,"LocationId":102,"Location":{"Id":102,"LocationName":"TestLocation","Room":{"Id":8,"RoomName":"TestRoom"}},...
But when we try a filtered call such as:
.../api/Tests$expand=Location($select=Id,LocationName;$expand=Room($select=Id,RoomName))&$filter=contains(Location/Room/RoomName,%27a%27))
All we get is the standard:
{"error":{"code":"NotFound","message":"The type 'Edm.String' is not an entity type. Only entity types support $select and $expand.\r\nParameter name: context"}}
There is surely some way of being able to filter from a multi-levelled expand, but I am somewhat of a newbie to Using OData, so any help would be appreciated!
Also, I believe we are using OData - 7.01.
Just answering my own question here, as it turns out the way we were doing it was correct.
The system was breaking in other areas down the pipe.

Microsoft Graph api - Missing 'none' (or not any) functionality for $filter

I'm trying to retrieve any message that I haven't specifically categorized but there isn't a none filter option for Microsoft Graph. I tried to invert the any filter by using the following call:
/v1.0/me/messages?$filter=categories/any(c:c ne 'MyCategory')
However, this will still return items containing 'MyCategory'. Is there another method by which I can retrieve items that don't have that category set on them? (Returning items with no category set at all is not exclusive enough).
I had contemplated using custom extensions, however, the filter for extensions also lacks a none option.
try this
im not sure is this is your request
https://graph.microsoft.com/v1.0/me/messages/?$select=categories
There really isn't a good way to do negative comparisons like this within the API itself.
The reason any doesn't work is that your query translates to "include the message if any of it's categories are not 'MyCategory'". That would include messages with no categories (i.e. null) as well as message with any other category (i.e. a message with both MyCategory and Blue Category would match the filter).
Your best bet is using Open Extensions. These allow you to add application-specific data to Graph resources that you can filter against.

Possible to select a specific entity without knowing the key in OData?

I have a problem where I need to select a specific value from a specific entity from a entity set, however, I need to do it in a way without knowing the key.
This is the query I actually need:
odata/..../picklistLabels(locale='en_GB',optionId=10819)/label
However I need to program it in a way so it automatically selects the label without knowing the optionId. Is there a way to do this in OData?
From your question, I think that you want to perform a navigation but you don't have a key. Unfortunately, this exact functionality isn't available, however, you can perform an expand like this:
odata/..../picklistLabels?$filter=locale eq 'en_GB' and optionId=10819&$expand=label
This will get you the same information that the other call would do but in a slightly different format, you would have to find the first element in the array and then get the label property to get that information
As an aside, if you have the option to change the server (I'm guessing not due to the sapui5 tag but it might be useful for other users) you could change the key. If the locale and the optionId are enough to identify the object, then you could make these into a composite key. Here is a link for an example within the WebAPI OData source code: https://github.com/OData/ODataSamples/tree/master/WebApi/v4/ODataCompositeKeySample

How can I use group membership to restrict results?

In an MVC application, for a given action that all users are authorized to perform, I would like to filter results based on the user's group membership. For instance ...
Users in GroupA should only see records pertaining to BuildingX.
Users in GroupB should only see records pertaining to BuildingY.
Users in GroupC should see all records.
I have no problem using authorization filters to restrict access to Actions, but I'm having a much harder time finding how to restrict access to data short of explicitly modifying statements every place where data is fetched.
Assuming your records are in a database, the roles membership model doesn't extend to the database out of the box. You can build a roles-based access control for your database, but you will likely save time using a simpler approach. For example, using code like this in your controller:
if (Roles.IsUserInRole("GroupA")) {
// Get data for GroupA.
}
// Display data...
A year later, working on a different but related issue, I found the EntityFramework.DynamicFilters package which does exactly what I need!

Resources