-1

I want to get all users that are member of a group (transitive). This call gets what I want:

https://graph.microsoft.com/v1.0/groups/{guid}/transitiveMembers/microsoft.graph.user

In my C# application I use the Graph API SDK. I know how you specify queryoptions, but I need an url segment instead of a query options.

At the moment I have this:

graphClient.Groups[guid].TransitiveMembers.Request().Select("id").GetAsync();

This returns all members, but not only users. So if someone know how to achieve this with the C# sdk please let me know.

FerronSW
  • 505
  • 4
  • 18

1 Answers1

0

The provided request:

GET https://graph.microsoft.com/v1.0/groups/{group-id}/transitiveMembers/microsoft.graph.user

could be constructed and executed via msgraph-sdk-dotnet like this:

var requestUrl = $"{graphClient.Groups[groupId].TransitiveMembers.Request().RequestUrl}/microsoft.graph.user";
var message = new HttpRequestMessage(HttpMethod.Get, requestUrl);
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(message);
var response = await graphClient.HttpProvider.SendAsync(message);

and the response de-serialized like this:

if (response.IsSuccessStatusCode)
{
    var content = await response.Content.ReadAsStringAsync();
    var json = JObject.Parse(content);
    var items = json["value"].Where(i =>  (string)i["@odata.type"] == null);
    var members = items.Select(item => item.ToObject<Microsoft.Graph.User>());

    foreach (var member in members)
    {
          Console.WriteLine(member.UserPrincipalName);                     
    }
}

Since the query /groups/{group-id}/transitiveMembers/microsoft.graph.user still returns the collection of all transitive members but only the properties for microsoft.graph.user object are retrieved meaning it is not working precisely the same way as $filter parameter, maybe a more straightforward way would be:

a) retrieve all the transitive members via request:

var members = await graphClient.Groups[groupId].TransitiveMembers.Request().GetAsync();

b) and filter by user type:

var userMembers = members.Where(m => m.ODataType == "#microsoft.graph.user").Cast<User>();
foreach (var member in userMembers)
{
     Console.WriteLine(member.UserPrincipalName);
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193