Create, Update or Delete Tasks on MS Exchange Server in C#

Various people create a list of activities to be performed, which is also known as a to-do list. Such lists are usually created on a paper, text editor, spreadsheets, etc. Microsoft also provides you the feature of creating and managing to-do lists and terms them as tasks. In this article, we will cover how to add, update, or delete tasks on MS Exchange Server programmatically in C#.

C# API to Create and Update Tasks on MS Exchange Server

To create, update, or delete tasks on MS Exchange Server, we will use Aspose.Email for .NET. It is a feature-rich API to create email client applications and work with MS Exchange Server without writing complex code. You can either download the API’s DLL or install it from NuGet using the following command.

PM> Install-Package Aspose.Email

Create a Task on MS Exchange Server in C#

The following are the steps to create tasks on MS Exchange Server in C# .NET.

The following code sample shows how to create tasks on MS Exchange Server using C#.

// 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 (or other properties)
task.Subject = "New-Test";
task.Status = ExchangeTaskStatus.InProgress;
// Create task
client.CreateTask(client.MailboxInfo.TasksUri, task);
view raw create-task.cs hosted with ❤ by GitHub

Update Task on MS Exchange Server in C#

You can also access and update the existing tasks on MS Exchange Server programmatically using C#. The following are the steps to perform this operation.

The following code sample shows how to update a task on MS Exchange Server in C#.

// 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 tasks info collection from exchange
ExchangeMessageInfoCollection tasks = client.ListMessages(client.MailboxInfo.TasksUri);
// Parse all the tasks 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
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);
}
view raw update-task.cs hosted with ❤ by GitHub

Delete Tasks on MS Exchange Server in C#

The following are the steps to delete tasks on MS Exchange server in C#.

The following code sample shows how to delete tasks from MS Exchange Server in C#.

// Create instance of ExchangeClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get tasks collection from exchange
ExchangeMessageInfoCollection tasks = client.ListMessages(client.MailboxInfo.TasksUri);
// Parse all the tasks 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 criteria
if (task.Subject.Equals("test"))
{
// Delete task from exchange
client.DeleteItem(task.UniqueUri, DeletionOptions.DeletePermanently);
}
}
view raw delete-task.cs hosted with ❤ by GitHub

C# API to Manage Exchange Server Tasks - Get a Free License

You can get a free temporary license to work with tasks on MS Exchange Server without evaluation limitations.

Conclusion

In this article, you have learned how to manage tasks on Microsoft Exchange Server. Particularly, you have seen how to add, update, or delete tasks on MS Exchange Server programmatically in C#. Alongside, you can explore the documentation to read more about Aspose.Email for .NET. Also, you can ask your questions via our forum.

See Also