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);
}
}
}