Converting EntryId to EwsID using Exchange Web Services (EWS)

I’m currently writing WCF Services and a VSTO Addin for Outlook. On the client I’m having Outlook MailItem objects containing an EntryID. On the server-side I’m using Exchange Web Service in WCF to find the email selected on the client. EWS is working with a different id called EwsId. To convert all the id’s there is a method called ConvertId that is exactly what you need. The problem was that I got a “Data is corrupt” exception during the convert operation using the following sample:

http://blogs.msdn.com/b/brijs/archive/2010/09/09/how-to-convert-exchange-item-s-entryid-to-ews-unique-itemid-via-ews-managed-api-convertid-call.aspx

Cause

The EntryId property value in the Outlook MailItem object on the client is not equal to the Ews IdFormat.EntryId. The correct enumeration value is IdFormat.HexEntryId.

Solution

The correct method to convert the client EntryId to EwsId is the following:


string ConvertHexEntryIdToEwsId(ExchangeService esb, string sID, string strSMTPAdd)
{
AlternateId objAltID = new AlternateId();
objAltID.Format = IdFormat.HexEntryId;
objAltID.Mailbox = strSMTPAdd;
objAltID.UniqueId = sID;

AlternateIdBase objAltIDBase = esb.ConvertId(objAltID, IdFormat.EwsId);
AlternateId objAltIDResp = (AlternateId)objAltIDBase;
return objAltIDResp.UniqueId;
}

Advertisements

8 thoughts on “Converting EntryId to EwsID using Exchange Web Services (EWS)

  1. Spencer says:

    Thanks a million. Same exact headache!

    • Bernhard says:

      You are welcome. Another hint from me: In the case that you store the EntryId in a database you should keep in mind that the value is CASE-SENSITIVE 😉

  2. B V says:

    Thanks. Definitely saved couple of days 🙂

  3. mojodk says:

    Hi, great article! Any idea how to get strSMTPAdd from MailItem?

    • Aaron Egger says:

      strSMTPAdd is just the mail address of your mailbox.
      If you are using an exchange profile you can try this one:
      this.Application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;

  4. Jason McDonald says:

    oh thank you thank you thank you.

  5. Aaron Egger says:

    This solution doesn’t work for me..

    I try to use the Application_ItemSend method that is fired by an event on sending mails.
    The first parameter defines an object that i can parse to a MailItem object.

    So from here i have an EntryId to work with..

    Now i want to get the mail from the exchange server.

    I use the same way to convert the id:

    AlternateId objAltID = new AlternateId();
    objAltID.Format = IdFormat.HexEntryId;
    objAltID.UniqueId = (Item as Outlook.MailItem).EntryID; //Item is the parameter of Application_ItemSend
    objAltID.Mailbox = this.Application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress; //gets my current Email Address
    AlternateIdBase objId = _service.ConvertId(objAltID, IdFormat.EwsId);

    Microsoft.Exchange.WebServices.Data.EmailMessage.Bind(_service, ((AlternateId)objId).UniqueId); //_service is the object that contains the EWS Service

    => Here it fails. The Id is unknown.

    To check out the real id i added an query to get the last mail:
    FindItemsResults mailItems = _service.FindItems(getFolder(WellKnownFolderName.SentItems).Id, new Microsoft.Exchange.WebServices.Data.ItemView(1));

    I compared both ids and there is a difference:
    “AAMkAGZlYWFiYmVhLTUwOWMtNDYwMC05MWIxLTY0YjEyODMzNWQ1MABGAAAAAADYy6oBO3KTR5xBRkT75keUBwDluySqdk/GTYaTDQaVa5AqAAAAKcRgAABJDad72R0pTYvDajWhvXn3AABuRG/fAAA=” => The correct one

    “AAMkAGZlYWFiYmVhLTUwOWMtNDYwMC05MWIxLTY0YjEyODMzNWQ1MABGAAAAAADYy6oBO3KTR5xBRkT75keUBwDluySqdk/GTYaTDQaVa5AqAAAAKcRpAABJDad72R0pTYvDajWhvXn3AABuRGnbAAA=” => The corrupted id from the ConvertId function

    • Bernhard says:

      Are sending the email with client code? Note that, it could be difficult to get the mail item directly after sending the mail, because it exists only at the client and it takes some time until it is available in Exchange.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: