Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access user.MemberOf with Microsoft Graph Client Library

Note: I originally posed this question in the client library repo and they responded that this is an issue in the service library, not the the .NET library.

During development we have been doing something like this to get a user's groups:

var user = await GraphClient.Users[userId].Request().Expand("memberOf").GetAsync();

The result of which was fed to a method that would use the presumably returned "NextPageRequest" object to get results beyond the current page. Our fake development user accounts, as well as early real users never had enough group memberships to require the NextPageRequest logic, and testing of it was forgotten about.

After getting users with 20+ groups it eventually became clear that making a request as detailed above returns one page worth of memberships but does not return a NextPageRequest to use in the options of subsequent requests. Your documentation around collections makes it seem like this is how it should be done.

As I'm sure you already know, a way that does work is like this:

List<Group> userGroups = await GraphClient.Users[userId].MemberOf.Request().GetAsync().CurrentPage.Where(p => p.GetType() == typeof(Microsoft.Graph.Group)).Cast<Microsoft.Graph.Group>().ToList();

This, as far as we have seen, returns all of the user's group memberships. If the intent is that Expand not be used with "memberOf", then it shouldn't work at all. Right now the old code worked fine for around 760 of the 800 users in the tenant, the remainder being the ones that write the checks :). If we were doing .Expand("memberOf") incorrectly, let me know please.

Also, GetMemberGroups works differently than MemberOf. What's the intent? Maybe doing something like GetMemberGroups(securityEnabledOnly = false, expandGroupInfo = false) might be clearer.

like image 240
frankfralick Avatar asked Oct 17 '22 20:10

frankfralick


1 Answers

After looking at it some, I found an answer that returns all the groups for the user.

public async Task<List<string>> GetCurrentUserGroups(GraphServiceClient graphClient)
    {
        var totalGroups = new List<string>();
        var groups = await graphClient.Me.MemberOf.Request().GetAsync();

        while (groups.Count > 0)
        {
            foreach (Group g in groups)
            {
                totalGroups.Add(g.DisplayName);
            }
            if (groups.NextPageRequest != null)
            {
                groups = await groups.NextPageRequest.GetAsync();
            }
            else
            {
                break;
            }
        }
        return totalGroups;
    }
like image 50
user3720939 Avatar answered Oct 29 '22 16:10

user3720939