GraphUser2016
GraphUser2016

Reputation: 43

Microsoft Graph API: How to tell if an attendee is a group

I need to get events from a calendar, and find out individual users in the event.

var graphServiceClient = new GraphServiceClient(...);
var events = graphServiceClient.Me.CalendarView.Request().GetAsync();
// ...
var attendee = events[0].Attendees[0];
// Is attendee a group or user?
// If a group, how do we expand it?

Upvotes: 3

Views: 1552

Answers (1)

Fei Xue
Fei Xue

Reputation: 14649

We can determine whether the mail address is a user or the group via retrieve the user/group. For example, we can get the specific group via the REST below:

https://graph.microsoft.com/v1.0/groups?$filter=mail+eq+'[email protected]'

If the email we provide is a user, then the response would have an empty value, otherwise it return the information for this group.

And to get the members of specific group, we can make a request with the id return by the above request:

https://graph.microsoft.com/v1.0/groups/{groupid}/members

And here is the code to get the group and its members for your reference. And I recommend you make the REST via HttpClient because it is more flexible and efficient.

public async Task GetGroup(string mailAddress) {

    var groups = await graphserviceClient.Groups.Request().Top(10).GetAsync();

    foreach (var group in groups.CurrentPage)
    {
        Console.WriteLine(group.Mail);
        if (mailAddress.Equals(group.Mail))
            return group.Id;
    }

    while (groups.NextPageRequest != null)
    {
        groups = await groups.NextPageRequest.GetAsync();

        foreach (var group in groups.CurrentPage)
        {
            Console.WriteLine(group.Mail);
            if (mailAddress.Equals(group.Mail))
                return group.Id;
        }
    }

    return null;

}

public async void GetMembers(string groupId)
{

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("Authorization", "bearer " + _accessToken);

    string serviceURL = String.Format("https://graph.microsoft.com/v1.0/groups/{0}/members?$select=mail", groupId);
    var response = client.GetAsync(serviceURL).Result;

    JObject json = JObject.Parse(response.Content.ReadAsStringAsync().Result);
    foreach (var mail in json["value"].Values("mail"))
    {
        Console.WriteLine(mail);
    }
}

update

We need to have the "Group.Read.All" scope to read the groups: enter image description here

Upvotes: 2

Related Questions