Tuesday, October 10, 2023

How to Create a Calendar Event Using Microsoft Graph API and C#

Microsoft Graph API is a powerful tool that allows developers to access a wide range of Microsoft 365 services, including Outlook and Calendar. In this blog post, we will walk you through the steps to create a calendar event using the Microsoft Graph API and C#. This integration can be incredibly useful for automating event scheduling and managing calendars within your applications.

Prerequisites

Before we dive into the code, you'll need the following:

  • Microsoft 365 Account: Ensure you have access to a Microsoft 365 account and have the necessary permissions to create calendar events.
  • Azure App Registration: Create an Azure App Registration to obtain the required client ID and client secret for authentication.
  • Visual Studio or Visual Studio Code: Use an IDE such as Visual Studio or Visual Studio Code with the necessary C# development tools installed.
  • Microsoft.Graph NuGet Package: Install the Microsoft.Graph NuGet package in your project to interact with Microsoft Graph API.

Step 1: Set Up Your Azure App Registratio

Navigate to the Azure portal and create a new App Registration and Note down the Application (client) ID, Tenant ID and generate a client secret. This information will be needed for authentication.

Step 2: Install Microsoft.Graph NuGet Package

In your C# project, install the Microsoft.Graph NuGet package using the package manager console or NuGet package manager in Visual Studio:

Step 3: Create GraphServiceClient

var clientSecretCredential = new ClientSecretCredential(tenantid, clientid, secret);

var _graphServiceClient = new GraphServiceClient(clientSecretCredential);

Step 4: Create a Calendar Event

Now that we have obtained the graphServiceClient, we can use it to create a calendar event using the Microsoft Graph API.

var newEvent = new Event

            {

                Subject = actionPlan.EventTitle,

                Start = new DateTimeTimeZone

                {

                    DateTime = actionPlan.EventStartTime.Value.ToString("o"),

                    TimeZone = TimeZoneInfo.Utc.Id

                },

                End = new DateTimeTimeZone

                {

                    DateTime = actionPlan.EventEndTime.Value.ToString("o"),

                    TimeZone = TimeZoneInfo.Utc.Id

                },

                Location = new Location

                {

                    DisplayName = actionPlan.EventLocation

                },


                Body = new ItemBody

                {

                    Content = notificationBody,

                    ContentType = BodyType.Html

                },


                Attendees = attendeeList,


                ResponseRequested = true


            };

var createdEvent = await graphServiceClient.Users[currentUser].Events.Request().AddAsync(newEvent);

Adjust the event details and date/time as needed for your specific use case.

Conclusion

You have now learned how to create a calendar event using the Microsoft Graph API and C#. Integrating Microsoft Graph API into your applications opens up a world of possibilities for automating and enhancing calendar management within your organization. Happy coding!

No comments:

Post a Comment

How to Create Events Using Microsoft Graph API with Delegated Permissions and MSAL.js

Microsoft Graph API provides powerful capabilities to interact with Microsoft 365 services. In this guide, we’ll explore how to create calen...