Exchange Web Services and MAPI Props

Merry Christmas! With Exchange 12 (whoops! Exchange 2007) just around the corner, we're starting to get questions about Exchange Web Services. I just helped a customer with a sample illustrating the use of AdditionalProperties to fetch arbitrary MAPI properties from folders and messages. I figured I should clean up the sample and share it with the world.

Comment #1: It wasn't clear to me how to generate the web service proxy in Visual Studio. I don't do development directly on my Exchange server, so it wasn't as easy as doing "Add Web Reference" and picking "Web Services on the local machine". I finally found a reference indicating you can use either of these:

 http(s)://MyServer/ews/services.wsdl
http(s)://MyServer/EWS/exchange.asmx?wsdl

Incidently, neither of these URLs is documented in the Exchange 2007 SDK. Maybe someone more familiar with web services would have found it obvious, but I certainly didn't. The SDK folks are working to correct this oversight.

Comment #2: The object ExchangeServiceBinding isn't documented in the SDK either. I had to borrow snippets from other samples to figure out how to use it. The SDK folks are working on that too.

Comment #3: The usual caveats for samples apply here - use at your own risk. Error handling is minimal, so if it crashes - it's not my fault. Enjoy!

 using System;
using System.Net;
using EWS; //this is the web reference

namespace GetProps
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
            ICredentials creds = new NetworkCredential("SomeUser", "SomePassword", "SomeDomain");

            exchangeServer.Credentials = creds;
            exchangeServer.Url = @"https://MyServer/EWS/Exchange.asmx";

            DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
            folderIDArray[0] = new DistinguishedFolderIdType();
            folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;

            PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType();
            ptuftDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;

            PathToExtendedFieldType pteftComment = new PathToExtendedFieldType();
            pteftComment.PropertyTag = "0x3004"; // PR_COMMENT
            pteftComment.PropertyType = MapiPropertyTypeType.String;

            GetFolderType myfoldertype = new GetFolderType();
            myfoldertype.FolderIds = folderIDArray;
            myfoldertype.FolderShape = new FolderResponseShapeType();
            myfoldertype.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
            myfoldertype.FolderShape.AdditionalProperties = new BasePathToElementType[2];
            myfoldertype.FolderShape.AdditionalProperties[0] = ptuftDisplayName;
            myfoldertype.FolderShape.AdditionalProperties[1] = pteftComment;

            Console.WriteLine("Getting inbox");
            GetFolderResponseType myFolder = exchangeServer.GetFolder(myfoldertype);

            FolderInfoResponseMessageType firmtInbox = 
                (FolderInfoResponseMessageType) myFolder.ResponseMessages.Items[0];

            Console.WriteLine("got folder: {0}",firmtInbox.Folders[0].DisplayName);

            if (null != firmtInbox.Folders[0].ExtendedProperty)
            {
                Console.WriteLine("Comment: {0}",firmtInbox.Folders[0].ExtendedProperty[0].Item.ToString());
            }
            else
            {
                Console.WriteLine("Comment: not found");
            }

            PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
            ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;

            PathToExtendedFieldType pteftFlagStatus = new PathToExtendedFieldType();
            pteftFlagStatus.PropertyTag = "0x1090"; // PR_FLAG_STATUS
            pteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;

            FindItemType findItemRequest = new FindItemType();
            findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
            findItemRequest.ItemShape = new ItemResponseShapeType();
            findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.IdOnly;
            findItemRequest.ItemShape.AdditionalProperties = new BasePathToElementType[2];
            findItemRequest.ItemShape.AdditionalProperties[0] = ptuftSubject;
            findItemRequest.ItemShape.AdditionalProperties[1] = pteftFlagStatus;
            findItemRequest.ParentFolderIds = new FolderIdType[1];
            findItemRequest.ParentFolderIds[0] = firmtInbox.Folders[0].FolderId;

            FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);

            foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
            {
                if (firmtMessage.RootFolder.TotalItemsInView > 0)
                {
                    foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
                    {
                        Console.WriteLine("got item: {0}",it.Subject);
                        if (null != it.ExtendedProperty)
                        {
                            Console.WriteLine("Prop 0x1090: {0}",it.ExtendedProperty[0].Item.ToString());
                        }
                        else
                        {
                            Console.WriteLine("Prop 0x1090: not found");
                        }
                    }
                }
            }

            Console.WriteLine("\nHit any key to continue");
            Console.ReadKey(true);
        }
    }
}

Comments

  • Anonymous
    January 23, 2007
    Here are some Exchange 2007 Web Services resources from around the web... Microsoft Resources VIDEO:

  • Anonymous
    January 30, 2007
    Question How do I access my Outlook contacts from my web application? Short Answer You don't, well at

  • Anonymous
    January 30, 2007
    Question How do I access my Outlook contacts from my web application? Short Answer You don't, well at

  • Anonymous
    March 18, 2007
    Can you post the services.wsdl online? I would like to see it without installing exchange 2007...

  • Anonymous
    June 09, 2008
    HOw to do this a mobile not using Outlook(hence no POOM)?

  • Anonymous
    June 09, 2008
    If it's possible to issue HTTP requests from your mobile, then you should be able to use a web service. So - however you would use a web service from there, you can adapt that to use EWS.

  • Anonymous
    June 24, 2008
    I am working on a problem where I want to decode a TNEF encoded MIME message. I am using the class Base64Decode in Microsoft.Exchange.data.Mime namespace and I am also able to decode the message if it doesn't contain an attachment . But , for a TNEF encoded message without attachment , the conversion fails. Do you have any suggestions ? Please help . In addition to that , could you provide me some sample code to parse a MIME message using MimeDocument,MimeReader and Tnef Reader classes? Thanks in advance

  • Anonymous
    October 02, 2008
    I've put together a list of articles which cover common questions on Exchange Web Services (EWS). These

  • Anonymous
    October 02, 2008
    I've put together a list of articles which cover common questions on Exchange Web Services (EWS). These

  • Anonymous
    May 06, 2009
    I have a big problem. I have a VB application that gets users from Exchange server, and gets their calendars using MAPI. Now I'm porting that application to .NET and I'm using Exchange Web Service API. The problem is that I can't take the PR_STORE_ENTRYID Property of the contact. Within the old code, I took it by AddressEntry.ID and I store that in database, and now I should take that StoreID with EWS API, but I don't know how. I know that I should use ExtendedProperties, and PropertyID = "Ox0FFB", but have no idea in which way :( Please, I appreciate if you can help me. Regards

  • Anonymous
    June 03, 2009
    The comment has been removed

  • Anonymous
    July 29, 2010
    I'm trying to use classes and methods documented in the Namespace ExchangeWebServices but when I add the Web reference can not get all methods and classes that allow me to do Bulk Transfer Operations  with ExportItems Operation and Operation UploadItems as shown in the documentation: http://msdn.microsoft .com/en-us/library/bb409286.aspx.