Home > Software design >  Get users from AAD group using Graph API using delta query
Get users from AAD group using Graph API using delta query

Time:07-01

I have the following code:

public async Task<IGroupDeltaCollectionPage> GetGroupMembersPageByIdAsync(string groupId)
{            
  return await graphClient.Groups.Delta().Request().Filter($"id  eq '{groupId}'")                                .Top(MaxResultCount).GetAsync();            
}
public async Task GetUsersPageAsync(string objectId)       
{           
   var users = await GetGroupMembersPageByIdAsync(objectId);            
   users.AdditionalData.TryGetValue("@odata.nextLink", out object nextLink);
   users.AdditionalData.TryGetValue("@odata.deltaLink", out object deltaLink);
   var nextPageUrl = (nextLink1 == null) ? string.Empty : nextLink.ToString();
   var deltaUrl = (deltaLink1 == null) ? string.Empty : deltaLink.ToString();   
}  

How do I get list of users from users variable?

CodePudding user response:

My code below get the members whose type is user and add the ids into a List. Pls note, members@delta maybe null. So my code is just a sample.

enter image description here

using Azure.Identity;
using Microsoft.Graph;
using Newtonsoft.Json.Linq;

public async Task<IActionResult> Index()
{
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var tenantId = "your_tenant_name.onmicrosoft.com";
    var clientId = "azure_ad_app_id";
    var clientSecret = "client_secret";
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    //get group members
    //var users = await graphClient.Groups["groupId"].TransitiveMembers.Request().Top(999).GetAsync();
    //get group member delta info
    var delta = await graphClient.Groups.Delta().Request().Filter("id eq 'group_id'").GetAsync();
    var currentPage = delta.CurrentPage;
    var a = currentPage[0].AdditionalData["members@delta"].ToString();
    string jsonStr = "{\"arr\":"   a   "}";

    List<string> UserMemberIds = new List<string>();
    JObject json = JObject.Parse(jsonStr);
    var arr = json["arr"].ToList();
    for (var i = 0; i < arr.Count; i  ) { 
        var type = arr[i]["@odata.type"].ToString();
        if (type == "#microsoft.graph.user") {
            UserMemberIds.Add(arr[i]["id"].ToString());
        }
    }
    return View();
}
  • Related