Need to select groups a user is a member of while selecting user id, proxyAddresses, mail RRS feed

  • Question

  • Given a user's email, I need to be able to retrieve their user object and select the following things:

    1. proxyAddresses
    2. mail
    3. group Ids user is a member of

    I've currently got the whole things working, but it's SLOW. I want to consolidate the graph queries so I'm not making repeated calls, slowing things down. Here's my code that retrieves a user's id:

    private string GetUserGuid(string email)
        if (string.IsNullOrWhiteSpace(email))
            return "";
        var username = email.Split('@')[0];
        var graphQuery = $"{_tenantId}/users?$filter=proxyAddresses/any(x:startswith(x,'smtp:{username}'))";
        var response = GenerateResponse(graphQuery);
        var json = JObject.Parse(response.Result.Content.ReadAsStringAsync().Result);
        var userId = json.SelectTokens("$.value[?(]").Select(j => j["id"].ToString()).FirstOrDefault();
        return userId;

    This does the job, but I don't really need the id value - I just need this user's data. I modified the graph query to this:

    var graphQuery = $"{_tenantId}/users?$select=proxyaddresses,mail,id&$filter=proxyAddresses/any(x:startswith(x,'smtp:{username}'))";

    And this works. I get all the user's email addresses (both primary and proxy addresses) from Active Directory, and the id (though I really don't need it now). Here's the problem - I need to grab all the group Ids the user belongs to at the same time. I can easily get that information with this query: 

    var userGroupIdsGraphQuery = $"{_tenantId}/users/{userId}/transitiveMemberOf";

    And this works, but I have to make another http request, and that just wastes time. I was hoping to find a "$select" option that would return a user's groups, but I haven't found it yet. I want to avoid multiple round trips if possible. Can anyone help me with this? 

    Thursday, March 28, 2019 11:38 PM