Booking a Teams Meeting using Microsoft Graph

Introduction

Scheduling a meeting is an easy task in Microsoft Teams but there may be situations where we will have to schedule Teams Meeting programmatically and share the link with attendees.

We can either use the Calendar API or the Cloud Communications API to create a Teams meeting invite.

Microsoft Graph provides the Calendar API to organize an event where meeting invitees can click a join URL, and attend the meeting online in Microsoft Teams or Skype. The calendar API has streamlined, built-in integration with Outlook calendar, which results in an online meeting event in the Outlook calendar.

However, if we want to create an event that is calendar agnostic and provides more programmatic support, we can go with the Cloud Communications API .

In this article, we will see how to use the Calendar API within C# based console application to create a Teams Meeting Invite and share it with attendees.

Register Azure App

As the first step for calling Graph API, we will need to take care of Authentication and Authorization which we will be doing by creating an Azure Application Registration. Let’s head over to Azure and search for App Registration and Click on New Registration. Specify the name and click on Register.

Once the App is created, we can note down the Client ID and Tenant ID as it will be used later in the code for Authentication.

Graphical user interface, text, application, email

Description automatically generated

Next, we need to create a Client Secret by heading over to Certificates & secrets -> New client secret. Specify the description and expiry and click on create which will add the client secret to the app. Copy the value before navigating away from the screen as it will be hashed later.

Graphical user interface, text, application, email

Description automatically generated

Once we have added the secret to the application, the final step in registration is to define the permission scope which authorizes the graph API access to specific resources in Microsoft 365. In this example, we will be creating a Teams Event in a user’s calendar and hence would be using the Event Resource of the Calendar API and issuing a POST request to the /users/{id | userPrincipalName}/events endpoint. As we are running the code from an unattended console application, we will be granting a Calendar.ReadWrite Application Permissions to work with the Calendar API.

Graphical user interface, text, application, email

Description automatically generated

Thus we have completed the Azure App registration and copied the Client ID, Client Secret, and Tenant ID which we will be using in the code to Authenticate the Graph Call

Code Walkthrough

We have added the Azure.Identity, Microsoft.Identity.Web and Microsoft.Graph references to work with the code.

As the first step, we will use the tenant Id, Client Id, Client Secret to create a ClientSecretCredential Object which will be used to authenticate against the Azure Active Directory. Using this, we will provision a GraphServiceClient

Text

Description automatically generated

The next step is to create the event object with the needed parameters like

A picture containing timeline

Description automatically generated

The OnlineMeetingProviderType attribute also has an option to setup a Skype for Business meeting

Finally, we will pass the event’s body to the Graph Service Client to issue the post request to a specific user’s calendar events which will schedule the Teams meeting. On Successful creation of the meeting, invites would be sent to all the attendees and an event will be created in the specified user’s (user used in the Graph Request) outlook. The meeting invite link will be present in the result return in the attribute – OnlineMeeting.JoinUrl

Graphical user interface, text, application

Description automatically generated

Sample Code

var appscopes = new[] { “https://graph.microsoft.com/.default” };
var tenantId = “b3629ed1-3361-4ec4-a2b7-5066a5c5fa07”;
var clientId = “3a260c3c-a99d-44ee-8147-c63e5093b2f3”;
var clientSecret = “hhK7Q~vbf1KHZF8sTClCh4hUYI3eqBjoJHNcw”;
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, appscopes);
var users = await graphClient.Users.Request().Select(x => new { x.Id, x.DisplayName }).GetAsync();
foreach (var user in users)
{
if (user.DisplayName == “Priyan”)
{
Console.WriteLine($”DisplayName, UserId”);
Console.WriteLine($”{user.DisplayName}, {user.Id}”);
}
}
Console.WriteLine();
var @event = new Event
{
Subject = “Code Review”,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = “Setting up a short meeting for code review”
},
Start = new DateTimeTimeZone
{
DateTime = “2021-12-02T12:00:00”,
TimeZone = “Pacific Standard Time”
},
End = new DateTimeTimeZone
{
DateTime = “2021-12-02T14:00:00”,
TimeZone = “Pacific Standard Time”
},
Location = new Location
{
DisplayName = “Da Vinci Meeting Room”
},
Attendees = new List<Attendee>()
{
new Attendee
{
EmailAddress = new EmailAddress
{
Address = “John.Jacob@gmail.com”,
Name = “John Jacobs”
},
Type = AttendeeType.Required
}, new Attendee
{
EmailAddress = new EmailAddress
{
Address = “kspriyaranjan@gmail.com”,
Name = “Test User”
},
Type = AttendeeType.Required
}
},
AllowNewTimeProposals = true,
IsOnlineMeeting = true,
OnlineMeetingProvider = OnlineMeetingProviderType.TeamsForBusiness
};
var meetingDetails = await graphClient.Users[“f0fdbc21-188e-4969-9046-3df60c62964c”].Calendar.Events
.Request()
.Header(“Prefer”, “outlook.timezone=\”Pacific Standard Time\””)
.AddAsync(@event);
var meetingURL = meetingDetails.OnlineMeeting.JoinUrl;
Console.WriteLine($”The meeting URL is {meetingURL}”);

Testing the Code

On running the code, we can see that the event has been scheduled and we can see the meeting link in the output.

Text

Description automatically generated

We can also see the invite sent via mail for the attendees

Graphical user interface, text, application, email

Description automatically generated

Summary

Thus, we saw how to use Microsoft Graph API to schedule a Teams meeting using the Calendar API and share the invite with the attendees

Related Articles

Author

Author

Priyaranjan KS is a Modern Workplace Architect primarily focused on developing and architecting solutions around Office 365,Power Platform and Azure.He is also a Microsoft Most Valuable Professional(MVP) and a Microsoft Certified Trainer(MCT)

Latest Articles