Partager via


Working with items by using the EWS Managed API 2.0

Last modified: October 13, 2012

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010

Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.

You can use the Microsoft Exchange Web Services (EWS) Managed API to work with items in a mailbox. When you are working with an item that has a class that represents the item's specific type (for example, EmailMessage, Appointment, MeetingRequest, Task, and so on), use an instance of the class that represents the item instead of the Item class. If the item that you are working with does not have a class that represents its specific type, you can use the base Item class to work with the item. This topic describes basic operations — creating, deleting, updating, moving, and copying — that you can perform on items in a mailbox.

To create an item in a mailbox

  • Because the Item class does not have a publicly available constructor, you must use the constructor for the specific item type that you want to create. For example, use the EmailMessage(Microsoft.Exchange.WebServices.Data.ExchangeService) constructor to create a new e-mail message, and the Contact(Microsoft.Exchange.WebServices.Data.ExchangeService) constructor to create a new contact.

For more information about how to create an item of a specific type, see the following topics: Creating attachments by using the EWS Managed API 2.0, Creating appointments and meetings by using the EWS Managed API 2.0, Creating and sending email messages by using the EWS Managed API 2.0, and Creating folders by using the EWS Managed API 2.0.

To delete an item in a mailbox

  1. Bind to the item to update. In the following example, the ItemId object named itemId identifies the item to update. The ExchangeService object named service contains the settings that specify how to bind to Exchange Web Services.

    Item item = Item.Bind(service, itemId);
    
  2. Delete the item, as shown in the following example.

    item.Delete(DeleteMode.MoveToDeletedItems);
    

To update an item in a mailbox

  1. Bind to the item to update. In the following example, the ItemId object named itemId identifies the item to update. The ExchangeService object named service contains the settings that specify how to bind to Exchange Web Services.

    Item item = Item.Bind(service, itemId);
    
  2. Change a property and update the item, as shown in the following example.

    item.Subject = "New subject for item";
    item.Update(ConflictResolutionMode.AutoResolve);
    

To move an item in a mailbox

  1. Bind to the item to update. In the following example, the ItemId object named itemId identifies the item to update. The ExchangeService object named service contains the settings that specify how to bind to Exchange Web Services.

    Item item = Item.Bind(service, itemId);
    
  2. Use the Move method to move the item. The following example shows how to move an item to the Drafts folder.

    item.Move(WellKnownFolderName.Drafts);
    

To copy an item in a mailbox

  1. Bind to the item to update. In the following example, the ItemId object named itemId identifies the item to update. The ExchangeService object named service contains the settings that specify how to bind to Exchange Web Services.

    Item item = Item.Bind(service, itemId);
    
  2. Copy the item. The following example shows how to copy an item to the Drafts folder.

    item.Copy(WellKnownFolderName.Drafts);
    

Compiling the code

Robust programming

  • Write appropriate error handling code for common search errors.

  • Review the client request XML that is sent to the Exchange server.

  • Review the server response XML that is sent from the Exchange server.

  • Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl method.

Security

  • Use HTTP with SSL for all communication between client and server.

  • Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.

  • Do not include user names and passwords in trace files.

  • Ensure that autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.