Manage Calendar & Meeting Requests with Exchange Web Services (EWS)
This article explains how to work with meeting requests and calendar items using Aspose.Email for C++ and Exchange Web Services (EWS). You will learn how to:
- Send meeting requests to one or multiple recipients
- Create, update, and cancel appointments
- List calendar items with paging support
- Add events to secondary calendars
- Share calendars with users
- Retrieve extended properties from calendar items
All scenarios include C++ code examples using IEWSClient. They use these headers and namespaces:
#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <AlternateView.h>
#include <Calendar/Appointment.h>
#include <Calendar/AppointmentCollection.h>
#include <Calendar/Recurrences/DailyRecurrencePattern.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiCalendar.h>
#include <Mapi/MapiNamedProperty.h>
#include <Mapi/MapiPropertyCollection.h>
#include <Mapi/MailConversionOptions.h>
#include <Mapi/PidNamePropertyDescriptor.h>
#include <Mapi/PropertyDataType.h>
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/WebService/Delegate/ExchangeDelegateUser.h>
#include <Clients/Exchange/WebService/Delegate/ExchangeDelegatePermissions.h>
#include <Clients/Exchange/WebService/Delegate/ExchangeDelegateFolderPermissionLevel.h>
#include <Clients/Exchange/ExchangeMailboxInfo.h>
#include <Clients/Exchange/ExchangeFolderInfo.h>
#include <Clients/Exchange/ExchangeFolderInfoCollection.h>
#include <Clients/Exchange/AppointmentPageInfo.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/guid.h>
#include <system/collections/ilist.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Calendar;
using namespace Aspose::Email::Calendar::Recurrences;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;
Send a Meeting Request via EWS
You can create and send a meeting request by building an Appointment object and attaching it to a MailMessage before sending it through IEWSClient.
The following code sample demonstrates how to create and send a recurring meeting request via Exchange Web Services:
- Create an Appointment and set the location, time, attendees.
- Add recurrence if needed - here a DailyRecurrencePattern that repeats the meeting every day for five days.
- Create an email message using the MailMessage class.
- Attach the meeting request via
AddAlternateView(); RequestApointment produces thetext/calendarpart. - Connect to the Exchange Server and send the message using the IEWSClient->Send(MailMessage) method.
try
{
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"bob@test.com"));
SharedPtr<Appointment> app = MakeObject<Appointment>(u"Meeting room 1", DateTime::get_Now().AddHours(1), DateTime::get_Now().AddHours(1.5),
MakeObject<MailAddress>(u"administrator@test.com"), attendees);
app->set_Summary(u"meeting request summary");
app->set_Description(u"description");
app->set_Recurrence(MakeObject<DailyRecurrencePattern>(DateTime::get_Now().AddDays(5)));
SharedPtr<MailMessage> msg = MakeObject<MailMessage>();
msg->set_From(MakeObject<MailAddress>(u"administrator@test.com"));
msg->set_To(attendees);
msg->set_HtmlBody(u"<h3>HTML Heading</h3><p>Email Message detail</p>");
msg->set_Subject(u"meeting request");
msg->AddAlternateView(app->RequestApointment());
client->Send(msg);
Console::WriteLine(u"Appointment request sent");
client->Dispose();
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
Create, Update, and Cancel Appointments
Aspose.Email provides dedicated IEWSClient methods to manage calendar items:
- CreateAppointment() stores the appointment in the calendar and returns its unique ID;
- FetchAppointment() reads an appointment back by that ID;
- UpdateAppointment() writes the changes of an appointment object;
- CancelAppointment() cancels it.
SetTimeZone attaches an IANA time zone to the start and end times. The sample walks through the whole life cycle of one appointment.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
DateTime now = DateTime::get_Now();
DateTime startTime(now.get_Year(), now.get_Month(), now.get_Day(), now.get_Hour(), 0, 0);
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"attendee@domain.com"));
SharedPtr<Appointment> app = MakeObject<Appointment>(u"Room 112", startTime, startTime.AddHours(1),
MakeObject<MailAddress>(u"organizer@domain.com"), attendees);
app->SetTimeZone(u"America/New_York");
app->set_Summary(u"Project review");
app->set_Description(u"Review of the project status");
// create
String uid = client->CreateAppointment(app);
// update
app->set_Location(u"Room 115");
app->set_Summary(u"New summary for " + app->get_Summary());
app->set_Description(u"New Description");
client->UpdateAppointment(app);
// read back
SharedPtr<Appointment> fetched = client->FetchAppointment(uid);
Console::WriteLine(u"Summary: " + fetched->get_Summary());
Console::WriteLine(u"Location: " + fetched->get_Location());
Console::WriteLine(u"Total appointments: " + Convert::ToString(client->ListAppointments()->get_Length()));
// cancel
client->CancelAppointment(app);
Console::WriteLine(u"Total appointments: " + Convert::ToString(client->ListAppointments()->get_Length()));
client->Dispose();
List Appointments with Paging Support
When a mailbox contains many appointments, paging helps retrieve items efficiently. For this purpose, Aspose.Email provides multiple overloads of the ListAppointmentsByPage() method, including ones that take a folder URI or a query. Each call returns an AppointmentPageInfo with the appointments of one page; the loop requests the next page until the last one is reached.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
try
{
const int32_t itemsPerPage = 2;
SharedPtr<AppointmentPageInfo> page = client->ListAppointmentsByPage(itemsPerPage);
int32_t retrievedItems = page->get_Items()->get_Count();
while (!page->get_LastPage())
{
page = client->ListAppointmentsByPage(itemsPerPage, page->get_PageOffset() + 1);
retrievedItems += page->get_Items()->get_Count();
}
Console::WriteLine(u"Appointments retrieved page by page: " + Convert::ToString(retrievedItems));
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();
Add Events to a Secondary Calendar Folder
Besides the default calendar, a mailbox can have further calendar folders. A folder created with the IPF.Appointment class under the calendar URI is such a secondary calendar. The appointment methods accept the URI of the folder to work in; alternatively, CurrentCalendarFolderUri makes a folder the default for all calls that do not name one.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
try
{
String calendarUri = client->get_MailboxInfo()->get_CalendarUri();
String folderName = u"New Calendar";
// look for the secondary calendar, create it when it does not exist yet
String newCalendarUri;
for (SharedPtr<ExchangeFolderInfo> folder : client->ListSubFolders(calendarUri))
{
if (folder->get_DisplayName() == folderName)
{
newCalendarUri = folder->get_Uri();
}
}
if (String::IsNullOrEmpty(newCalendarUri))
{
newCalendarUri = client->CreateFolder(calendarUri, folderName, nullptr, u"IPF.Appointment")->get_Uri();
}
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"attendee@domain.com"));
DateTime start = DateTime::get_Now().AddDays(1);
SharedPtr<Appointment> appointment = MakeObject<Appointment>(u"Room 121", start, start.AddHours(1),
MakeObject<MailAddress>(u"from@domain.com"), attendees);
appointment->set_Summary(u"Event in the secondary calendar");
// the folder named in every call
client->CreateAppointment(appointment, newCalendarUri);
appointment->set_Location(u"Room 122");
client->UpdateAppointment(appointment, newCalendarUri);
Console::WriteLine(u"Appointments in the secondary calendar: " + Convert::ToString(client->ListAppointments(newCalendarUri)->get_Length()));
client->CancelAppointment(appointment, newCalendarUri);
// or the folder set once as the current calendar
client->set_CurrentCalendarFolderUri(newCalendarUri);
client->CreateAppointment(appointment);
Console::WriteLine(u"Appointments in the current calendar: " + Convert::ToString(client->ListAppointments()->get_Length()));
client->CancelAppointment(appointment);
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();
Share Calendar Invitation
Microsoft Exchange server can share a calendar with another user of the same server by sending a sharing invitation. With Aspose.Email this takes two steps: DelegateAccess gives the user a permission on the calendar folder - here the Reviewer level of ExchangeDelegateFolderPermissionLevel - and CreateCalendarSharingInvitationMessage produces the invitation, which is sent as a TNEF message so that Outlook recognizes it.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
// give the user read access to the calendar
SharedPtr<ExchangeDelegateUser> delegateUser = MakeObject<ExchangeDelegateUser>(u"sharingto@domain.com", ExchangeDelegateFolderPermissionLevel::NotSpecified);
delegateUser->get_FolderPermissions()->set_CalendarFolderPermissionLevel(ExchangeDelegateFolderPermissionLevel::Reviewer);
client->DelegateAccess(delegateUser, u"sharingfrom@domain.com");
// create the invitation and send it
SharedPtr<MapiMessage> invitation = client->CreateCalendarSharingInvitationMessage(u"sharingto@domain.com");
SharedPtr<MailConversionOptions> options = MakeObject<MailConversionOptions>();
options->set_ConvertAsTnef(true);
client->Send(invitation->ToMailMessage(options));
client->Dispose();
Retrieve Extended Properties from Calendar Items
Calendar items may carry custom MAPI properties that a plain appointment does not expose. FetchMapiCalendar reads the items as MapiCalendar objects and requests the properties described by the given property descriptors as well. The sample asks for a named property K1 of type Integer32 in the appointment property set; ListItems supplies the URIs of all items of the calendar.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<String> uriList = client->ListItems(client->get_MailboxInfo()->get_CalendarUri());
// the named property K1 (32-bit integer) of the appointment property set
SharedPtr<PropertyDescriptor> descriptor = MakeObject<PidNamePropertyDescriptor>(u"K1", PropertyDataType::Integer32,
Guid(u"00020329-0000-0000-C000-000000000046"));
auto calendars = client->FetchMapiCalendar(uriList, MakeArray<SharedPtr<PropertyDescriptor>>({ descriptor }));
for (SharedPtr<MapiCalendar> calendar : calendars)
{
for (auto entry : calendar->get_NamedProperties())
{
SharedPtr<MapiNamedProperty> named = DynamicCast_noexcept<MapiNamedProperty>(entry.get_Value());
if (named != nullptr && named->get_NameId() == u"K1")
{
Console::WriteLine(calendar->get_Subject() + u": K1 = " + Convert::ToString(named->GetInt32()));
}
}
}
client->Dispose();