Getting multiple users/groups by objectids

6k views Asked by At

I have a list of objectids of users/groups, and I would like to resolve them to display names and profile photos. Instead of making a separate request for each objectid, I would like to batch them and get all the results in one response. Is there an API available for that? From the searched that I've done, I couldn't find anything like that. If such an API doesn't exists, are there any tips to make this efficient?

Thanks, Boaz

3

There are 3 answers

2
Dan Kershaw - MSFT On BEST ANSWER

This is a capability that we will be adding very soon (a new action called getById), that will allow you to specify up to 1000 objects to fetch from Microsoft Graph (although restricted for now to directory objects). This is similar to functionality that is currently supported in Azure AD Graph https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/functions-and-actions#getObjectsByObjectIds.

Will update this thread when this new action is available.

Hope this helps,

0
RasmusW On

I don't think its possible.

It seems the $filter query parameter doesn't like "or" expressions when filtering on id.

For example: https://graph.microsoft.com/beta/users/?$filter=(usageLocation eq 'US') and (city eq 'Redmond') works fine.

But when the id property is added to the expression like this: https://graph.microsoft.com/beta/users/?$filter=(usageLocation eq 'US') and (city eq 'Redmond') or (id eq 'f19096bf-a58c-46ba-9ffd-0344f1daecf8')

Then it fails with Unsupported or invalid query filter clause specified for property 'id' of resource 'User'.

If you can find some common ground between the objects you want to retrieve, you may be able to get them in batches.

0
Cédric Bloamrt On

Maybe Dan forgot the thread. Effectively getById is the way to go.

Here is how i get all groups for a user:

var groupIds = new List<string>();
var groups = awai client.Users[userId].GetMemberGroups(false).Request().PostAsync();
groupIds.AddRange(groups);
while(groups.NextPageRequest != null)
{
    groups = await groups.NextPageRequest.PostAsync();
    groupIds.AddRange(groups);
}
var groupNames = new List<string>();
var groupQuery = await client.DirectoryObjects.GetByIds(groupIds,new[] { "group" }).Request().Select("DisplayName").PostAsync();
foreach (Group group in groupQuery)
{
    groupNames.Add(group.DisplayName);
}
while (groupQuery.NextPageRequest != null)
{
    groupQuery = await groupQuery.NextPageRequest.PostAsync();
    foreach (Group group in groupQuery)
    {
        groupNames.Add(group.DisplayName);
    }
}