Deleting Items (Exchange Web Services)
Topic Last Modified: 2009-07-16
You can use Exchange Web Services to delete items from a mailbox. The DeleteItem request is formed the same way when you are deleting messages, contacts, tasks, and calendar items.
Example
The following example shows how to delete a message item in the Exchange store.
static void Main()
{
// Create the bindings and set the credentials.
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Url = "http://myExchangeServer/ews/exchange.asmx";
esb.Credentials = new NetworkCredential("username", "password", "domain");
// Create the DeleteItem request.
DeleteItemType dit = new DeleteItemType();
dit.ItemIds = new BaseItemIdType[1];
// Create an item ID type and set the ID.
ItemIdType itemId = new ItemIdType();
// Get the ID from the message that you obtained by using FindItem or CreateItem.
itemId.Id = "AQAtAE";
dit.ItemIds[0] = itemId;
// Delete the message.
DeleteItemResponseType diResponse = esb.DeleteItem(dit);
// Check the result.
if (diResponse.ResponseMessages.Items.Length > 0 &&
diResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
{
Console.WriteLine("Message with Id {0} is deleted.", itemId.Id);
}
}
The following example shows the XML request message that is sent from the client to the server.
<?xml version="1.0" encoding="utf-8"?>
<DeleteItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
DeleteType="HardDelete">
<ItemIds xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
<ItemId Id="AQAtAE"
xmlns="https://schemas.microsoft.com/exchange/services/2006/types" />
</ItemIds>
</DeleteItem>
The following example shows the XML response message that is sent from the server to the client.
<?xml version="1.0" encoding="utf-8"?>
<DeleteItemResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ResponseMessages xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
<DeleteItemResponseMessage ResponseClass="Success">
<ResponseCode>NoError</ResponseCode>
</DeleteItemResponseMessage>
</ResponseMessages>
</DeleteItemResponse>
The SOAP messages that are passed between the Exchange Web Services client and server are defined by the XML schema and WSDL files. The XML schema and WSDL files define the contract between the client and server. Proxy class generators create an object-model abstraction of those SOAP messages, which can simplify programming. This code example uses a proxy class library that was generated by Microsoft Visual Studio 2005. Different proxy class generators create different object models for a given Web service. This proxy class code example is an illustration only. Refer to the proxy class generator documentation for support for proxy classes.
Note
The item identifier has been shortened to preserve readability. Item identifiers are returned in the FindItem, CreateAttachment, CreateItem (when the item is saved and not sent), GetAttachment, GetEvents, GetItem, and UpdateItem responses.
Compiling the Code
For information about compiling the code, see Exchange Web Services Client Development.