Filter Appointments from Exchange Server
Aspose.Email for C++ enables developers to filter calendar appointments on Microsoft Exchange using the IEWSClient and ExchangeQueryBuilder APIs. You can filter appointments by date range, recurrence state, or other appointment properties to retrieve only the events you need.
The samples in this article use these headers and namespaces:
#include <Calendar/Appointment.h>
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/Search/ExchangeQueryBuilder.h>
#include <Clients/Exchange/Search/AppointmentQueryBuilder.h>
#include <Tools/Search/MailQuery.h>
#include <Tools/Search/DateComparisonField.h>
#include <Tools/Search/BoolComparisonField.h>
#include <system/array.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email::Calendar;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;
using namespace Aspose::Email::Tools::Search;
Filter Appointments by Date Range
The Appointment member of ExchangeQueryBuilder offers the fields of a calendar item. Conditions on its Start and End dates select the appointments of a period; GetQuery turns them into a MailQuery that ListAppointments evaluates on the server.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<ExchangeQueryBuilder> builder = MakeObject<ExchangeQueryBuilder>();
builder->get_Appointment()->get_Start()->Since(DateTime(2025, 9, 15));
builder->get_Appointment()->get_End()->BeforeOrEqual(DateTime(2025, 10, 10));
ArrayPtr<SharedPtr<Appointment>> appointments = client->ListAppointments(builder->GetQuery());
Console::WriteLine(u"Appointments in the period: " + Convert::ToString(appointments->get_Length()));
client->Dispose();
Filter Recurring or Non-Recurring Appointments
The IsRecurring field separates series from single events. The sample lists the appointments that are not part of a series.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<ExchangeQueryBuilder> builder = MakeObject<ExchangeQueryBuilder>();
builder->get_Appointment()->get_IsRecurring()->Equals(false);
ArrayPtr<SharedPtr<Appointment>> appointments = client->ListAppointments(builder->GetQuery());
for (SharedPtr<Appointment> appointment : appointments)
{
Console::WriteLine(appointment->get_Summary() + u" - " + appointment->get_StartDate().ToString());
}
client->Dispose();