user3587624
user3587624

Reputation: 1471

Get Owner(s) in .NET Azure Active Directory groups

I have an email account which is a Security Group where there are few members in it. I am trying to figure out the email address of the owner of the group but I haven't been able to figure it out.

Below is the source code

    ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async () => await Task.FromResult(accessToken));
    var group = (Group)await activeDirectoryClient.Groups.Where(u => u.Mail == "email@domaincom").ExecuteSingleAsync();.
    var groupFetcher = activeDirectoryClient.Groups.GetByObjectId(group.ObjectId);
    var membersResult = groupFetcher.Members.ExecuteAsync().Result;
    var ownerResult = groupFetcher.Owners.ExecuteAsync().Result;

After I execute this code, I can see the members of the Group but why ownerResult variable is always empty? How can I retrieve the owner of the group?

Upvotes: 0

Views: 375

Answers (1)

Fei Xue
Fei Xue

Reputation: 14649

I am testing using the code below(Microsoft.Azure.ActiveDirectory.GraphClient with version 2.1.1)and it works well for me. Please ensure that the group have the owners assigned.

var group = (Group) client.Groups.Where(u => u.Mail == "[email protected]").ExecuteSingleAsync().Result;
var groupFetcher = client.Groups.GetByObjectId(group.ObjectId);
//var membersResult = groupFetcher.Members.ExecuteAsync().Result;
var ownerResult = groupFetcher.Owners.ExecuteAsync().Result;
foreach (var owner in ownerResult.CurrentPage)
    Console.WriteLine(((Microsoft.Azure.ActiveDirectory.GraphClient.User)owner).DisplayName);

You can check it from Azure portal like figure below: enter image description here

If the owner exists, I also suggest that you capture the request using Fiddler to check whether the response is expected.

Upvotes: 2

Related Questions