Create, Manage, and Delete Exchange Server Tasks with EWS

Aspose.Email supports processing tasks on Exchange 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 EWSClient class exposes functions like CreateTask, UpdateTask, and DeleteTask which are used to process tasks on Exchange. 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 Tasks

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Create Exchange task object
ExchangeTask task = new ExchangeTask();
// Set task subject and status to In progress
task.Subject = "New-Test";
task.Status = ExchangeTaskStatus.InProgress;
client.CreateTask(client.MailboxInfo.TasksUri, task);

Specify 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-.NET
client.TimezoneId = "Central Europe Standard Time";

Update Tasks

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create and initialize credentials
var credentials = new NetworkCredential("username", "12345");
// Create instance of ExchangeClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get all tasks info collection from exchange
ExchangeMessageInfoCollection tasks = client.ListMessages(client.MailboxInfo.TasksUri);
// Parse all the tasks info in the list
foreach (ExchangeMessageInfo info in tasks)
{
// Fetch task from exchange using current task info
ExchangeTask task = client.FetchTask(info.UniqueUri);
// Update the task status to NotStarted
task.Status = ExchangeTaskStatus.NotStarted;
// Set the task due date
task.DueDate = new DateTime(2013, 2, 26);
// Set task priority
task.Priority = MailPriority.Low;
// Update task on exchange
client.UpdateTask(task);
}

Delete Tasks

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of ExchangeClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get all tasks info collection from exchange
ExchangeMessageInfoCollection tasks = client.ListMessages(client.MailboxInfo.TasksUri);
// Parse all the tasks info in the list
foreach (ExchangeMessageInfo info in tasks)
{
// Fetch task from exchange using current task info
ExchangeTask task = client.FetchTask(info.UniqueUri);
// Check if the current task fulfills the search criteria
if (task.Subject.Equals("test"))
{
//Delete task from exchange
client.DeleteItem(task.UniqueUri, DeletionOptions.DeletePermanently);
}
}

Send Task Requests

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-.NET
string dataDir = RunExamples.GetDataDir_Exchange();
// Create instance of ExchangeClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
MsgLoadOptions options = new MsgLoadOptions();
options.PreserveTnefAttachments = true;
// load task from .msg file
MailMessage eml = MailMessage.Load(dataDir + "task.msg", options);
eml.From = "firstname.lastname@domain.com";
eml.To.Clear();
eml.To.Add(new MailAddress("firstname.lastname@domain.com"));
client.Send(eml);

Save Tasks 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-.NET
ExchangeTask task = new ExchangeTask();
task.Subject = "TASK-ID - " + Guid.NewGuid();
task.Status = ExchangeTaskStatus.InProgress;
task.StartDate = DateTime.Now;
task.DueDate = task.StartDate.AddDays(3);
task.Save(dstEmail);

List Tasks

IEWSClient provides the ListTasks method that can be used to fetch tasks from an Exchange Web Service. It has several overloads that can be used to retrieve the list of tasks from a specific folder or using some search criteria. The below code sample illustrates getting all or specific tasks from the Tasks folder.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Set mailboxURI, Username, password, domain information
string mailboxUri = "https://ex2010/ews/exchange.asmx";
string username = "test.exchange";
string password = "pwd";
string domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);
IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);
//Listing Tasks from Server
client.TimezoneId = "Central Europe Standard Time";
TaskCollection taskCollection = client.ListTasks(client.MailboxInfo.TasksUri);
//print retrieved tasks' details
foreach (ExchangeTask task in taskCollection)
{
Console.WriteLine(client.TimezoneId);
Console.WriteLine(task.Subject);
Console.WriteLine(task.StartDate);
Console.WriteLine(task.DueDate);
}
//Listing Tasks from server based on Query - Completed and In-Progress
ExchangeQueryBuilder builder = new ExchangeQueryBuilder();
ExchangeTaskStatus[] selectedStatuses = new ExchangeTaskStatus[]
{
ExchangeTaskStatus.Completed,
ExchangeTaskStatus.InProgress
};
builder.TaskStatus.In(selectedStatuses);
MailQuery query = builder.GetQuery();
taskCollection = client.ListTasks(client.MailboxInfo.TasksUri, query);
//print retrieved tasks' details
foreach (ExchangeTask task in taskCollection)
{
Console.WriteLine(client.TimezoneId);
Console.WriteLine(task.Subject);
Console.WriteLine(task.StartDate);
Console.WriteLine(task.DueDate);
}

Filter Tasks

Aspose.Email provides the capability to retrieve specific tasks from the server instead of retrieving all tasks from the server. The API can be used to retrieve tasks by task status such as Completed, Deferred, In Progress, Not started or Waiting on others. The ExchangeQueryBuilder class can be used to specify the desired criterion utilizing the Status property. It also allows specifying multiple conditions for retrieving desired tasks from Exchange Server. This is demonstrated by the following code sample.