Good morning people,
I'm to get all the teams members of the Microsoft Teams teams in the organization of the user who signing on my web.
The login was made with GraphServiceClient
Once the user login, I save the graphClient instance and in my controller I have this code:
public async Task<IActionResult> Index()
{
List<UserModel> users = new List<UserModel>();
var result = await graphClient.Teams.GetAsync();
foreach (var team in result.Value)
{
var members = await graphClient.Teams[team.Id].Members.GetAsync();
foreach (var member in members.Value)
{
var user = await graphClient.Users[member.UserId].GetAsync();
UserModel userModel = new UserModel(user.Id, user.DisplayName, user.Mail, user.Department, user.JobTitle, user.MobilePhone, user.OfficeLocation, "");
users.Add(userModel);
}
}
return View(users);
}
But when I run the same query in Microsoft Graph Explorer, I notice the object member must contain a UserId property.
And if that's not enough, debugging the solution, I can notice effectively my object member contain a UserId property.
So where is the problem?



That's because the response value
memberis type ofConversationMember, and it's the base type ofAadUserConversationMember. It's also the base type for the below types. We can see more details here.In your sceenshoot, we could see that your are getting a member which is type of
AadUserConversationMemberand that's why it hasUserIdproperty, but if the member is AnonymousGuestConversationMember, it doesn't contain aUserIdproperty.Generally speaking, the
memberscollection we get might be made up with several types of data, some of them has property UserId but some of them might not, they are all team members, to include them all, we abstract a base typeConversationMemberand it only containsIdproperty instead of UserId which is only available for some of the types.If we are trying to get all user type members' profile, we demand to add a validate like code below. But please note, I haven't test it out about the
if clauseas I don't have the test result, I just show my idea here. We might also add filter when getting the members to only get AadUserConversationMember type of members returned(Maybe with request like thisGET https://graph.microsoft.com/v1.0/teams/teamsId/members?$filter=(microsoft.graph.aadUserConversationMember/userId ne null)).