مدیریت تقویم و درخواست‌های جلسه با Exchange Web Services (EWS)

این مقاله توضیح می‌دهد چگونه با درخواست‌های جلسه و آیتم‌های تقویم با استفاده از Aspose.Email برای C++ و Exchange Web Services (EWS) کار کنید. شما خواهید آموخت چگونه:

  • ارسال درخواست‌های جلسه به یک یا چند گیرنده
  • ایجاد، به‌روزرسانی و لغو قرارها
  • فهرست آیتم‌های تقویم با پشتیبانی از صفحه‌بندی
  • رویدادها را به تقویم‌های ثانویه اضافه کنید
  • تقویم‌ها را با کاربران به اشتراک بگذارید
  • دریافت ویژگی‌های گسترش یافته از آیتم‌های تقویم

تمام سناریوها شامل مثال‌های کد C++ با استفاده از IEWSClient. آن‌ها این سرصفحه‌ها و فضاهای نام را استفاده می‌کنند:

#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;

ارسال درخواست جلسه از طریق EWS

می‌توانید یک درخواست جلسه را با ساخت یک Appointment شیء و پیوست آن به یک MailMessage قبل از ارسال آن از طریق IEWSClient.

نمونه کد زیر نشان می‌دهد چگونه یک درخواست جلسه تکراری را از طریق Exchange Web Services ایجاد و ارسال کنید:

  1. یک Appointment و مکان، زمان، شرکت‌کنندگان را تنظیم کنید.
  2. در صورت نیاز تکرار را اضافه کنید - اینجا یک DailyRecurrencePattern که جلسه را هر روز به مدت پنج روز تکرار می‌کند.
  3. یک پیام ایمیل را با استفاده از MailMessage کلاس.
  4. درخواست جلسه را از طریق AddAlternateView(); RequestApointment نتیجه را تولید می‌کند text/calendar قسمت.
  5. به سرور Exchange متصل شوید و پیام را با استفاده از IEWSClient->Send(MailMessage) متد.
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());
}

ایجاد، به‌روزرسانی و لغو قرارها

Aspose.Email امکانات ویژه‌ای را فراهم می‌کند IEWSClient روش‌ها برای مدیریت آیتم‌های تقویم:

  • CreateAppointment() قرار ملاقات را در تقویم ذخیره می‌کند و شناسه یکتا را برمی‌گرداند؛
  • FetchAppointment() یک قرار ملاقات را با آن شناسه می‌خواند؛
  • UpdateAppointment() تغییرات شیء قرار ملاقات را می‌نویسد؛
  • CancelAppointment() آن را لغو می‌کند.

SetTimeZone یک منطقه زمانی IANA را به زمان‌های شروع و پایان الصاق می‌کند. نمونه تمام چرخه زندگی یک قرار ملاقات را قدم به قدم نشان می‌دهد.

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();

فهرست‌گذاری قرارها با پشتیبانی از صفحه بندی

زمانی که یک صندوق‌پست شامل تعداد زیادی قرار باشد، صفحه‌بندی به بازیابی کارآمد آیتم‌ها کمک می‌کند. برای این منظور، Aspose.Email چندین overload از ListAppointmentsByPage() متد، شامل متدهایی که یک URI پوشه یا یک پرس‌و‌جو می‌پذیرند. هر فراخوانی یک AppointmentPageInfo با قرار ملاقات‌های یک صفحه؛ حلقه صفحه بعدی را درخواست می‌کند تا زمانی که آخرین صفحه برسد.

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();

اضافه کردن رویدادها به یک پوشه تقویم ثانویه

علاوه بر تقویم پیش‌فرض، یک صندوق‌نامه می‌تواند پوشه‌های تقویم دیگری داشته باشد. پوشه‌ای که با IPF.Appointment کلاس تحت URI تقویم تقویم ثانویه‌ای است. متدهای قرار ملاقات URI پوشه‌ای را می‌پذیرند که در آن کار می‌شود؛ به‌جای آن، CurrentCalendarFolderUri پوشه‌ای را به‌عنوان پیش‌فرض برای تمام فراخوانی‌هایی که نامی ندارند، تعیین می‌کند.

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();

به‌اشتراک‌گذاری دعوتنامه تقویم

سرور Microsoft Exchange می‌تواند تقویم را با کاربر دیگری از همان سرور به‌وسیله ارسال دعوت‌نامه به اشتراک بگذارد. با Aspose.Email این کار در دو مرحله انجام می‌شود: DelegateAccess به کاربر اجازه دسترسی به پوشه تقویم می‌دهد - اینجا Reviewer سطح ExchangeDelegateFolderPermissionLevel - و CreateCalendarSharingInvitationMessage دعوت‌نامه را تولید می‌کند، که به‌صورت پیام TNEF ارسال می‌شود تا Outlook آن را تشخیص دهد.

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();

دریافت ویژگی‌های گسترش یافته از آیتم‌های تقویم

آیتم‌های تقویم ممکن است ویژگی‌های سفارشی MAPI داشته باشند که یک قرار ملاقات ساده آن را نشان ندهد. FetchMapiCalendar آیتم‌ها را به عنوان MapiCalendar شی‌ها و خواص توصیف‌شده توسط توصیف‌گرهای ویژگی داده‌شده را نیز درخواست می‌کند. نمونه یک ویژگی نام‌گذاری‌شده را درخواست می‌کند K1 از نوع Integer32 در مجموعه ویژگی‌های قرار ملاقات؛ ListItems URIهای تمام آیتم‌های تقویم را فراهم می‌کند.

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();