Frank Mehlhop
Frank Mehlhop

Reputation: 2222

How to get a user from an IGraphServiceClient object?

I have a POST method and want to get a user from an IGraphServiceClient object (in the Graph.Microsoft package and namespace). The GET method works fine. Then I take a user from this list and set them as a parameter for my POST method.

    public async Task<Dictionary<string, List<string>>> GetUserGroupsAsync(ICollection<string> userIds)
    {
        var aggregatedUserGroupMap = new Dictionary<string, List<string>>();
        foreach (string userId in userIds)
        {
            try
            {
                var userMemberOfCollectionRequest = graphServiceClient.Users[userId].MemberOf.Request();
                var userMemberOfCollection = await userMemberOfCollectionRequest.GetAsync().ConfigureAwait(false);
                if (!aggregatedUserGroupMap.ContainsKey(userId)) { aggregatedUserGroupMap.Add(userId, new List<string>()); }
                foreach (var memberOf in userMemberOfCollection) { aggregatedUserGroupMap[userId].Add(memberOf.Id); }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        return aggregatedUserGroupMap;
    }

The values in the incoming string collection, userIds, are user email addresses, copied from the GET result.

The value of userMemberOfCollectionRequest looks fine. The RequestUrl property contains "https://graph.microsoft.com:443/v1.0/users/[email protected]/memberOf". Headers and QueryOptions are empty collections.

In the above method, the following line throws an exception:

var userMemberOfCollection = await userMemberOfCollectionRequest.GetAsync().ConfigureAwait(false);

The exception message reads:

Request_ResourceNotFound

Resource '[email protected]' does not exist or one of its queried reference-property objects are not present.

at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken) at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption) at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption) at Microsoft.Graph.UserMemberOfCollectionWithReferencesRequest.GetAsync(CancellationToken cancellationToken) at xxx.xxx.BusinessComponent.GraphBC.GetUserGroupsAsync(ICollection`1 userIds) in C:\workspace\xxx\xxx.xxx\xxx.xxx.Core\BusinessComponent\GraphBC.cs:line 50

Does anyone have an idea for where I should look to solve this problem?

Upvotes: 0

Views: 350

Answers (1)

Frank Mehlhop
Frank Mehlhop

Reputation: 2222

The solution was not to use the email, instead using the ObjectId property (a GUID instead of an email).

Upvotes: 0

Related Questions