![Create, Update or Delete Tasks on MS Exchange Server in C#](images/aspose-email-exchange.png#center)
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#.
- .NET API to Manage Tasks on MS Exchange Server
- Create a Task on MS Exchange Server in C#
- Update a Task on MS Exchange Server in C#
- Delete Tasks on MS Exchange Server 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.
- First, connect to Exchange Server and get the instance of the EWS client into an IEWSClient object.
- Then, create an instance of ExchangeTask class.
- Set properties of tasks such as subject, status, etc.
- Finally, create task using IEWSClient.CreateTask(IEWSClient.MailboxInfo.TasksUri, ExchangeTask) method.
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); |
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.
- First, connect to Exchange Server and get the instance of the EWS client into an IEWSClient object.
- Then, get lists of tasks into an ExchangeMessageInfoCollection object using IEWSClient.ListMessages(IEWSClient.MailboxInfo.TasksUri) method.
- Loop through each ExchangeMessageInfo in the collection.
- Fetch each ExchangeTask using IEWSClient.FetchTask(ExchangeMessageInfo.UniqueUri) method and filter the required one(s).
- Finally, edit properties and update the task using IEWSClient.UpdateTask(ExchangeTask) method.
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); | |
} |
Delete Tasks on MS Exchange Server in C#
The following are the steps to delete tasks on MS Exchange server in C#.
- First, connect to Exchange Server and get the instance of the EWS client into an IEWSClient object.
- Then, get lists of tasks into an ExchangeMessageInfoCollection object using IEWSClient.ListMessages(IEWSClient.MailboxInfo.TasksUri) method.
- Loop through each ExchangeMessageInfo in the collection.
- Fetch each ExchangeTask using IEWSClient.FetchTask(ExchangeMessageInfo.UniqueUri) method and filter the required one(s).
- Finally, delete the task using IEWSClient.DeleteItem(ExchangeTask.UniqueUri, DeletionOptions.DeletePermanently) method.
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); | |
} | |
} |
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.