Web Services Exchange 2010 - Rechercher des emails en positionnant une restriction


Bonjour,

Voici un exemple clé en main d'utilisation des Web Services Exchange 2010 avec deux petites subtilités :

- La première permet de définir plusieurs dossiers et/ou plusieurs boites aux lettres pour un seul appel Web services comme FindItem

DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1]; folderIDArray[0] = new DistinguishedFolderIdType(); folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox; folderIDArray[0].Mailbox = new EmailAddressType(); folderIDArray[0].Mailbox.EmailAddress = "sebastien.bovo@microsoft.com"; // Add folders to the request. findItemRequest.ParentFolderIds = folderIDArray;

- La deuxième consiste à pouvoir positionner une ou plusieurs restrictions sur les éléments que l’on souhaite récupérer. La syntaxe n’est pas facile à appréhender : c’est donc agréable d’avoir un exemple sous la main. Dans le code ci-dessous je ne récupère que les emails non lus

// Add a restriction to get for example only emails that are unread PathToUnindexedFieldType messageIsReadPath = new PathToUnindexedFieldType(); messageIsReadPath.FieldURI = UnindexedFieldURIType.messageIsRead; IsEqualToType IsEqualForUnreadFlag = new IsEqualToType(); IsEqualForUnreadFlag.Item = messageIsReadPath; FieldURIOrConstantType IsReadConstant = new FieldURIOrConstantType(); ConstantValueType IsReadConstantValue = new ConstantValueType(); IsReadConstantValue.Value = "0"; // "0" for false and "1" for true IsReadConstant.Item = IsReadConstantValue; IsEqualForUnreadFlag.FieldURIOrConstant = IsReadConstant; // Create the restriction array RestrictionType restriction = new RestrictionType(); AndType andType = new AndType(); andType.Items = new SearchExpressionType[] { IsEqualForUnreadFlag }; restriction.Item = andType; // Add the restriction to the findItemRequest object using in the EWS call findItemRequest.Restriction = restriction;

 

--

Voici le code complet dans une application console :

static void Main(string[] args) { // Create the proxy for Exchange Web services ExchangeServiceBinding esb = new EWS.ExchangeServiceBinding(); // Give the credentials of the current user as we are in a console application esb.Credentials = System.Net.CredentialCache.DefaultCredentials; // Give the production url to use esb.Url = "https://emea.mail.microsoft.com/EWS/exchange.asmx"; esb.RequestServerVersionValue = new RequestServerVersion(); esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010_SP1; // Call our methods using the proxy created Console.WriteLine("Get unread emails in the inbox"); Console.WriteLine("------------------------------"); FindItemsInInbox(esb); Console.WriteLine("Get recurring tasks between two dates"); Console.WriteLine("------------------------------"); FindReccursiveTasks(esb, DateTime.Parse("2009/09/01"), DateTime.Parse("2010/10/27")); Console.ReadKey(); } static void FindItemsInInbox(ExchangeServiceBinding esb) { // Form the FindItem request. FindItemType findItemRequest = new FindItemType(); findItemRequest.Traversal = ItemQueryTraversalType.Shallow; // Define which item properties are returned in the response. ItemResponseShapeType itemProperties = new ItemResponseShapeType(); itemProperties.BaseShape = DefaultShapeNamesType.AllProperties; // Add properties shape to the request. findItemRequest.ItemShape = itemProperties; // Identify which folders to search to find items. // In folderIDArray you can add several DistinguishedFolderIdType objects. // For each object you have to specify: // .Id --> referencing the folder you would like to use // .Mailbox --> giving the email of the user mailbox DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1]; folderIDArray[0] = new DistinguishedFolderIdType(); folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox; folderIDArray[0].Mailbox = new EmailAddressType(); folderIDArray[0].Mailbox.EmailAddress = "sebastien.bovo@microsoft.com"; // Add folders to the request. findItemRequest.ParentFolderIds = folderIDArray; // Add a restriction to get for example only emails that are unread PathToUnindexedFieldType messageIsReadPath = new PathToUnindexedFieldType(); messageIsReadPath.FieldURI = UnindexedFieldURIType.messageIsRead; IsEqualToType IsEqualForUnreadFlag = new IsEqualToType(); IsEqualForUnreadFlag.Item = messageIsReadPath; FieldURIOrConstantType IsReadConstant = new FieldURIOrConstantType(); ConstantValueType IsReadConstantValue = new ConstantValueType(); IsReadConstantValue.Value = "0"; // "0" for false and "1" for true IsReadConstant.Item = IsReadConstantValue; IsEqualForUnreadFlag.FieldURIOrConstant = IsReadConstant; // Create the restriction array RestrictionType restriction = new RestrictionType(); AndType andType = new AndType(); andType.Items = new SearchExpressionType[] { IsEqualForUnreadFlag }; restriction.Item = andType; // Add the restriction to the findItemRequest object using in the EWS call findItemRequest.Restriction = restriction; try { // Send the request and get the response. Console.WriteLine("Connecting to EWS..."); FindItemResponseType findItemResponse = esb.FindItem(findItemRequest); // Get the response messages. ResponseMessageType[] rmta = findItemResponse.ResponseMessages.Items; foreach (ResponseMessageType rmt in rmta) { // Cast to the correct response message type. if (((FindItemResponseMessageType)rmt).ResponseClass == ResponseClassType.Success) { FindItemResponseMessageType firmt = (FindItemResponseMessageType)rmt; ArrayOfRealItemsType aorit = (ArrayOfRealItemsType)firmt.RootFolder.Item; ItemType[] it = aorit.Items; Console.WriteLine("Items found. Nb = " + it.Length + ". Here is the list:"); foreach (ItemType i in aorit.Items) { Console.WriteLine("- " + i.Subject); } } } } catch (Exception e) { Console.WriteLine(e.Message); } }

 

A bientôt,

Sebastien.

Comments (0)

Skip to main content