Welcome to MSDN Blogs Sign in | Join | Help

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 = @"http://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);
        }
    }
}
Published Tuesday, December 19, 2006 10:21 PM by Stephen Griffin

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# FYI: Exchange 2007 Web Services Around the Web

Here are some Exchange 2007 Web Services resources from around the web... Microsoft Resources VIDEO:

Tuesday, January 23, 2007 5:45 PM by The CDOs and CDONTS of Messaging Development

# How do I access my Outlook contacts from my web application?

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

Tuesday, January 30, 2007 10:48 PM by Wes' Puzzling Blog

# How do I access my Outlook contacts from my web application?

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

Tuesday, January 30, 2007 10:49 PM by Wes' Blog

# re: Exchange Web Services and MAPI Props

Can you post the services.wsdl online? I would like to see it without installing exchange 2007...

Monday, March 19, 2007 1:49 AM by Mani

# re: Exchange Web Services and MAPI Props

HOw to do this a mobile not using Outlook(hence no POOM)?

Monday, June 09, 2008 7:36 PM by Mudit

# re: Exchange Web Services and MAPI Props

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.

Monday, June 09, 2008 8:17 PM by Stephen Griffin

# re: Exchange Web Services and MAPI Props

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

Tuesday, June 24, 2008 8:18 AM by Shilpa Bhasin

# Links on Common Exchange Web Services (EWS) Questions

I've put together a list of articles which cover common questions on Exchange Web Services (EWS). These

Thursday, October 02, 2008 5:02 PM by Dan's WebDAV 101

# Links on Common Exchange Web Services (EWS) Questions

I've put together a list of articles which cover common questions on Exchange Web Services (EWS). These

Thursday, October 02, 2008 7:55 PM by Dan's WebDAV 101

# re: Exchange Web Services and MAPI Props

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

Thursday, May 07, 2009 3:49 AM by Renata

# re: Exchange Web Services and MAPI Props

I'm currently trying to include exchange webservices 2007 functionality in a custom web application which is running on a Windows 2008 Server (IIS 7, Exchange Server 2007).

How do I access exchange 2007 webservices from my web application ? I currently use the defaultcredentials and receive an error, saying I don't have the required authorization (error 401).

Stating the credentials explicitly in the code works perfectly fine when trying to execute the code remotely, whether including the domain or not.

When executing the code locally on either the web server or my client computer, no problems occur. The web application I intend to write is supposed to run on the server and be accessed from any computer via my company's intranet.

The code looks something like this..

ExchangeServiceBinding binding = new ExchangeServiceBinding();

binding.Url = @"https://server/ews/exchange.asmx";

binding.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

CreateItemType createItemType = new CreateItemType();

.. (creating item and setting it's properties)

try

{

 CreateItemResponseType createItemResponse = binding.CreateItem(createItemType); <- the error seems to occur here  }  catch (Exception ex)  {

 Response.Write(ex.ToString()); <- results in error "unauthorized"

}

The authentication settings in IIS 7 are as follows:

anonymous authentication: deactivated

ASP.NET multiple identities: activated

digest authentication: deactivated

forms authentication: deactivated

standard authentication: deactivated

windows authentication: deactivated

User permissions set:

SYSTEM: full access rights

(windows user): full access rights

IUSR: full access

Wednesday, June 03, 2009 11:55 AM by Sven Hornberg

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker