How to figure out if a Graph users calendar is available via the API

HaakonS 21 Reputation points
2022-09-29T16:19:19.887+00:00

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?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,798 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,369 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bhanu Kiran 3,526 Reputation points
    2022-10-04T07:58:55.263+00:00

    Hi @HaakonS ,

    As per this documentation, you can only get one user calendar at once using graph API, Therefore you will have to query calendar of each user using users/{user ID}/calendar.
    247296-calendar.png

    /users?$select=calendar is not a valid endpoint. List Users API does not return the calendar of users, refer this document to know the supported properties.

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment

    0 comments No comments