Reading Calendar Events from Office 365 using Graph API and C#

Outlook calendar is part of the Outlook messaging hub in Office 365 that also lets you manage emails and contacts, find information about users in an organization, initiate online conversations, share files, and collaborate in groups. Microsoft Graph is the gateway to data and intelligence in Microsoft 365. It provides a unified programmability model that you can use to access the tremendous amount of data in Office 365, Windows 10, and Enterprise Mobility + Security.

The Microsoft Graph API offers a single endpoint, https://graph.microsoft.com, to provide access to rich, people-centric data and insights exposed as resources of Microsoft 365 services. You can use REST APIs or SDKs to access the endpoint and build apps that support scenarios spanning across productivity, collaboration, education, security, identity, access, device management, and much more.

Reading a events from an Office 365 user using Graph API

Initially an application needs to be registered with Azure. Once logged in go to App registration and new App registration.

Required permissions need to be set from the API permissions. For this example the permissions are set as below.

From certificates and secret tab, generate a secret. Note down the App ID, Tenant ID and Secret.

In Visual Studio, create a console application using C#

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Cal02
{
    class Program
    {
        private static List<Microsoft.Graph.Calendar> _ListCalendars;

     
        static void Main(string[] args)
        {
            MainAsync().Wait();
                       
         }

        static async Task MainAsync()
        {
            string mailid = "user@domain.com";

            string[] scopes = new string[] { "User.Read", "Calendars.Read" };
            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
            .Create("xxxx-xxxxxx-xxxx-xxxxxx-xxxxx-xxxx")
            .WithTenantId("xxxx-xxxxx-xxxxxx-xxxxx-xxxxxx-xxxxx")
            .WithClientSecret("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
            .Build();

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
            GraphServiceClient graphClient = new GraphServiceClient(authProvider);


            var me = await graphClient.Users[$"{mailid}"].Request().GetAsync();
            Console.WriteLine(me.DisplayName.ToString());


            var calendars = await graphClient
                     .Users[$"{mailid}"]
                     .Calendars
                     .Request()
                     .GetAsync();

            Console.WriteLine($"Number of calendars: {calendars.Count}");

            _ListCalendars = calendars.ToList();

            // Define the time span for the calendar view.
            List<QueryOption> options = new List<QueryOption>();
            options.Add(new QueryOption("startDateTime", DateTime.Now.ToString("o")));
            options.Add(new QueryOption("endDateTime", DateTime.Now.AddDays(7).ToString("o")));


            foreach (Microsoft.Graph.Calendar oCalendar in _ListCalendars)
            {
                Console.WriteLine("==================================================================================");
                string strCalendarInfo = $"Calendar Name: {oCalendar.Name}";
                strCalendarInfo += $"\nCalendar Id: {oCalendar.Id}";
                Console.WriteLine(strCalendarInfo);

                ICalendarEventsCollectionPage events = graphClient.Users[$"{mailid}"]
                .Calendars[$"{oCalendar.Id}"]
                .Events.Request(options).GetAsync().Result;

                Console.WriteLine("Events for this calendar:");
                foreach (Event current in events)
                {
                    Console.WriteLine($"Startdate: {current.Start.DateTime} Subject: {current.Subject}");
                }

            }

            Console.ReadKey();
        }

    }
}

Upon running the application the calendar events from the users calendar’s will be displayed.