Microsoft Graph API:如何判断与会者是否为一组

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

我需要从日历中获取事件,并找出事件中的个人用户。

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?

我们可以通过检索user/group来判断邮件地址是用户还是组。例如,我们可以通过下面的REST获取具体的组:

https://graph.microsoft.com/v1.0/groups?$filter=mail+eq+'group1@yourtenant.onmicrosoft.com'

如果我们提供的电子邮件是一个用户,那么响应将是一个空值,否则它return这个组的信息。

而要获取特定组的成员,我们可以通过上面的请求发出id为return的请求:

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

这里是获取群组和群组成员的代码,供大家参考。我建议您通过 HttpClient 制作 REST,因为它更灵活和高效。

public 异步任务 GetGroup(字符串邮件地址) {

    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);
    }
}

更新

我们需要 "Group.Read.All" 范围来读取组: