Exchange Web Service folder search

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

Exchange Web Service EWS provides for sure powerful search mechanism. Unfortinatelly building EWS consumers is not very trivial task, because query implementation is not well documented and it is not very intuitive. That means you need too much time to build simpe things. One good example is following code snippet. It shows one method which search for all folders (up to 1024 –defined by  MaxEntriesReturned and Denominator) with specified name (folderName).
For example if you want to search by containment string of exact match you will need to use property ContainmetMode.
Etc., etc. All other pieces of code whown below do not contain very much information (entropy low).

How this helps anyone to save a few hours.

   private static List<BaseFolderType> findFolder(ExchangeServiceBinding serviceProxy,
                       string folderName, bool exactMatch)
        {
            // Create the request and specify the traversal type.
           
            FindFolderType findFolderRequest = new FindFolderType();
            findFolderRequest.Traversal = FolderQueryTraversalType.Deep;

            // Define the properties to be returned in the response.
            FolderResponseShapeType responseShape = new FolderResponseShapeType();
            responseShape.BaseShape = DefaultShapeNamesType.Default;
            findFolderRequest.FolderShape = responseShape;

            // Identify which folders to search.
            DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
            folderIDArray[0] = new DistinguishedFolderIdType();
            folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;

            // In what folders to search
            findFolderRequest.ParentFolderIds = folderIDArray;

            // SEarch by folder name.
            RestrictionType restriction = new RestrictionType();
            PathToUnindexedFieldType restrict = new PathToUnindexedFieldType();
            restrict.FieldURI = UnindexedFieldURIType.folderDisplayName;
         
            // Build simple search operator
            ContainsExpressionType contains = new ContainsExpressionType();
            contains.ContainmentMode = exactMatch ? ContainmentModeType.FullString : ContainmentModeType.Substring;
            contains.ContainmentModeSpecified = true;
            contains.ContainmentComparison = ContainmentComparisonType.IgnoreCase;
            contains.ContainmentComparisonSpecified = true;
            contains.Item = restrict;
            contains.Constant = new ConstantValueType();
            contains.Constant.Value = folderName ;
            restriction.Item = contains;

            findFolderRequest.Restriction = restriction;

            // Define the paging scheme for the result set.
            FractionalPageViewType fpvt = new FractionalPageViewType();
            fpvt.MaxEntriesReturned = 1024;
            fpvt.MaxEntriesReturnedSpecified = true;
            fpvt.Numerator = 1; // Don’t try to find documentation for this
            fpvt.Denominator = 1024; // Don’t try to find documentation for this.
            findFolderRequest.Item = fpvt;

            // Send the request and get the response.
            FindFolderResponseType findFolderResponse = serviceProxy.FindFolder(findFolderRequest);

            // Get the response messages.
            ResponseMessageType[] rmta = findFolderResponse.ResponseMessages.Items;

            List<BaseFolderType> result = new List<BaseFolderType>();
            foreach (ResponseMessageType rmt in rmta)
            {
                FindFolderResponseMessageType ffrmt = (rmt as FindFolderResponseMessageType);

                FindFolderParentType ffpt = ffrmt.RootFolder;
                BaseFolderType[] folders = ffpt.Folders;

                result.AddRange(folders);
            }

            return result;
        }

 

 


Posted May 31 2010, 10:56 AM by Damir Dobric
developers.de is a .Net Community Blog powered by daenet GmbH.