Working with Tasks on Exchange Server

Working with Tasks

Aspose.Email supports processing tasks on Exchange Server using the ExchangeTask class. Different properties exposed by ExchangeTask, like Subject, Status, DueDate, and Priority, can be used to configure the task on Exchange. The IEWSClient class exposes functions like CreateTaskUpdateTask, and DeleteTask which are used to process tasks on the Exchange Server. This article shows how to:

  • Create a new task.
  • Set a task’s timezone.
  • Update a task.
  • Delete a task.
  • Send Task Request
  • Save Task to Disc

Create New Task

The following code snippet shows you how to use create a new task.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
// Create instance of EWSClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
// Create Exchange task object
System::SharedPtr<ExchangeTask> task = System::MakeObject<ExchangeTask>();
// Set task subject and status to In progress
task->set_Subject(u"New-Test");
task->set_Status(Aspose::Email::Clients::Exchange::WebService::ExchangeTaskStatus::InProgress);
client->CreateTask(client->get_MailboxInfo()->get_TasksUri(), task);

Specifying Timezone

The IEWSClient interface and ExchangeTask provide the TimeZoneId property for setting timezone information when creating a task. The following code snippet shows you how to specify Timezone.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
client->set_TimezoneId(u"Central Europe Standard Time");

Update Task

The following code snippets show how to update a task on the Exchange server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
// Create instance of ExchangeClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
// Get all tasks info collection from exchange
System::SharedPtr<ExchangeMessageInfoCollection> tasks = client->ListMessages(client->get_MailboxInfo()->get_TasksUri());
// Parse all the tasks info in the list
for (auto info : System::IterateOver(tasks))
{
// Fetch task from exchange using current task info
System::SharedPtr<ExchangeTask> task = client->FetchTask(info->get_UniqueUri());
// Update the task status to NotStarted
task->set_Status(Aspose::Email::Clients::Exchange::WebService::ExchangeTaskStatus::NotStarted);
// Set the task due date
task->set_DueDate(System::DateTime(2013, 2, 26));
// Set task priority
task->set_Priority(Aspose::Email::MailPriority::Low);
// Update task on exchange
client->UpdateTask(task);
}

Delete Task

The following code snippet shows you how to delete a task on the Exchange server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
// Create instance of ExchangeClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
// Get all tasks info collection from exchange
System::SharedPtr<ExchangeMessageInfoCollection> tasks = client->ListMessages(client->get_MailboxInfo()->get_TasksUri());
// Parse all the tasks info in the list
for (auto info : System::IterateOver(tasks))
{
// Fetch task from exchange using current task info
System::SharedPtr<ExchangeTask> task = client->FetchTask(info->get_UniqueUri());
// Check if the current task fulfills the search criteria
if (System::ObjectExt::Equals(task->get_Subject(), u"test"))
{
//Delete task from exchange
client->DeleteTask(task->get_UniqueUri(), Aspose::Email::Clients::Exchange::WebService::DeleteTaskOptions::DeletePermanently);
}
}

Sending Task Request

Aspose.Email Exchange service provides the capability to send task requests similar to Outlook. The following code snippet shows you how to load a task request message from the disc and send it using the IEWSClient.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::String dataDir = GetDataDir_Exchange();
// Create instance of ExchangeClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
System::SharedPtr<MsgLoadOptions> options = System::MakeObject<MsgLoadOptions>();
options->set_PreserveTnefAttachments(true);
// load task from .msg file
System::SharedPtr<MailMessage> eml = MailMessage::Load(dataDir + u"task.msg", options);
eml->set_From(MailAddress::to_MailAddress(u"firstname.lastname@domain.com"));
eml->get_To()->Clear();
eml->get_To()->Add(System::MakeObject<MailAddress>(u"firstname.lastname@domain.com"));
client->Send(eml);

Saving Task to Disc

Aspose.Email also allows saving Exchange Tasks to disc in Outlook MSG format. The following code snippet shows you how to save a task to disc.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<ExchangeTask> task = System::MakeObject<ExchangeTask>();
task->set_Subject(System::String(u"TASK-ID - ") + System::Guid::NewGuid());
task->set_Status(Aspose::Email::Clients::Exchange::WebService::ExchangeTaskStatus::InProgress);
task->set_StartDate(System::DateTime::get_Now());
task->set_DueDate(task->get_StartDate().AddDays(3));
System::StaticCast<Calendar::Task>(task)->Save(dstEmail);