I am writing a C# program that uses the Graph api. A tenant has users for whom the calendar may or may not be accessable to me. Because some users calendars has been restricted using this approach.
My question is what is the best way for me via the API to know if the user does in fact have a calendar visible to me?
I request the users with this call, but even though I specify that I want the field "u.Calendar" populated, all Calendars turn out as NULL in the resulting data-set:
var users = await AppClient.Users
.Request()
.Select(u => new
{
u.DisplayName,
u.Id,
u.Mail,
u.Calendar,
u.CalendarGroups,
u.Department,
u.JobTitle
})
.Top(500)
.OrderBy("DisplayName")
.GetAsync();
I do not want to bother with users for whom I cannot access the calendar and my straight forward approach of filtering this out would be to pick all users in this set that had u.Calendar != null. This I cannot do, because they are all null. So I try a roundabout way where I try and access each and every calendar directly and ignore the ones that give me exception:
var resultList = new List<User>();
foreach (var user in users)
{
try
{
await AppClient.Users[user.Id].Calendar
.Request()
.GetAsync();
resultList.Add(user);
}
catch (Exception e)
{
// Ignore users that we do not have calendar access to.
}
}
return userList;
This, however seems pretty nasty, as it generates a bunch of extra API calls and also I experience that it runs extremely slow. Only with 100 users it seems to take half a minute if 70% of the users case exceptions.
To sum up: Is there a better way to know if a user has calender access? Or perhaps to filter out only users with active calendars from the query alltogether?