Tuesday, 22 August 2017

EWS - Create a meeting or appointment in other user calendar.

Using Exchange Web Services (EWS) managed api, we can access and add event to specific user's calendar either by delegate access or impersonation. Both methods are used in different scenario and they require different set of permissions.

A user or account that has been granted impersonation rights will have the same rights as the user whom they are impersonating. Typically, service accounts are given the ability to impersonate the mailbox owner. In that case, the impersonating account has full mailbox rights, just as the mailbox owner does.

With delegate access, the delegate can be granted more granular rights, up to and including full mailbox access. Delegate access can also be configured per folder, or per mailbox. For example, a user can grant the delegate read-only access to the Inbox, read-write access to a calendar folder, and so on. For more info : Impersonation vs Delegate Access

Before proceed, you have to download Microsoft Exchange Web Services Managed API 2.0 dll and add as reference in your C# project.

Create a meeting or appointment as a delegate in C# using EWS

You can use following C# code to create new event in any specific user's calendar. This sample assumes that the delegate user has been granted the appropriate permissions for the mailbox owner's Calendar folder.
ExchangeService exchService = new ExchangeService(ExchangeVersion.Exchange2013_SP1); 
exchService.UseDefaultCredentials = false; 
exchService.Credentials = new NetworkCredential("admin@o365domain.com", "pwd"); 
exchService.Url = new Uri("https://outlook.office365.com/Ews/Exchange.asmx"); 
exchService.PreAuthenticate = false; 
  
Appointment appointment = new Appointment(exchService); 
// Set the properties on the appointment object to create the appointment. 
appointment.Subject = "Sales Meeting"; 
appointment.Body = "Focus on pre-sale and marketing."; 
appointment.Start = DateTime.Now.AddDays(2); 
appointment.End = DateTime.Now.AddDays(2).AddHours(3); 
appointment.Location = "Room 111"; 
appointment.ReminderMinutesBeforeStart = 240; 

appointment.RequiredAttendees.Add("user3@o365domain.onmicrosoft.com"); 

// Save the meeting to the Calendar folder of the mailbox owner and send the meeting request.
// This method call results in a CreateItem call to EWS.
Folder calendar_Folder = Folder.Bind(exchService, new FolderId(WellKnownFolderName.Calendar, "user1@o365domain.com")); 
appointment.Save(calendar_Folder.Id, SendInvitationsMode.SendToNone); 

if (appointment.RequiredAttendees.Count > 0)
{
   // The appointment has attendees so send them the meeting request.
   appointment.Save(calendar_Folder.Id, SendInvitationsMode.SendToAllAndSaveCopy);
}
else
{
    // The appointment does not have attendees, so just save to calendar.
    appointment.Save(calendar_Folder.Id, SendInvitationsMode.SendToNone);
}
Please check this MSDN post : Add appointments by using Exchange impersonation to create appointment by using impersonation.

Advertisements
Advertisements

No comments:

Post a Comment