Searching for your search folders?

Search folders in Exchange are a very powerful tool, but there is one quirk that often hangs developers up when they try to use them: “How do you create a search folder that can be seen in Outlook?”. Creating a search folder that is viewable in Outlook is important because it allows you to debug your search restriction and also allows you to present a search folder that an end user can interact with through Outlook or OWA.

The magic dust to creating a search folder that is viewable in Outlook is to make sure that you create it under the “Finder” folder which sits in the root of your mailbox heirarchy. If the folder is created anywhere else it will not be viewable in Outlook, though will contain the items that were specified in the query and can be access via EWS. The code below creates a search folder under the “Finder” folder which is referenced with the DistinguishedFolderIdNameType.searchfolders enumeration. The code below creates a search folder under the “Finder” folder and looks for all messages in your deleted items folder with “OOF” anywhere in a string in the subject. (For those of you that are wondering what an “OOF” is, it is what all we Microsofties call being “Out of Office”, supposedly the feature used to be called “Out of Facility”).

After this folder is created it will show up in Outlook online mode and OWA immediately, Outlook cached mode may take a bit longer to sync down the search folder, but it will eventually be populated as well.

ExchangeServiceBinding esb = new ExchangeServiceBinding();

esb.Url = "https://yoururl/ews/exchange.asmx";

esb.Credentials = CredentialCache.DefaultNetworkCredentials;

FolderIdType folderID = new FolderIdType();

       

//Now create the request that will create a new searchfolder under the finder directory

CreateFolderType folderType = new CreateFolderType();

SearchFolderType[] folderArray = new SearchFolderType[1];

folderArray[0] = new SearchFolderType();

folderArray[0].SearchParameters = new SearchParametersType();

folderArray[0].SearchParameters.Traversal = SearchFolderTraversalType.Shallow;

folderArray[0].SearchParameters.TraversalSpecified = true;

folderArray[0].SearchParameters.BaseFolderIds = new DistinguishedFolderIdType[1];

DistinguishedFolderIdType dType = new DistinguishedFolderIdType();

dType.Id = new DistinguishedFolderIdNameType();

dType.Id = DistinguishedFolderIdNameType.deleteditems; //search in the deleted items folder

folderArray[0].SearchParameters.BaseFolderIds[0] = dType;

PathToUnindexedFieldType path = new PathToUnindexedFieldType();

path.FieldURI = UnindexedFieldURIType.itemSubject; //search on subject

folderArray[0].SearchParameters.Restriction = new RestrictionType();

ContainsExpressionType expressionType = new ContainsExpressionType();

expressionType.Item = path;

expressionType.ContainmentMode = ContainmentModeType.Substring; //seach for substrings

expressionType.ContainmentModeSpecified = true;

expressionType.ContainmentComparison = ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;

expressionType.Constant = new ConstantValueType();

expressionType.Constant.Value = "OOF"; //search for any string containing "OOF"

folderArray[0].SearchParameters.Restriction.Item = expressionType;

folderArray[0].DisplayName = "OOF Search"; //Name the searchfolder OOF Search

folderType.Folders = folderArray;

TargetFolderIdType targetFolder = new TargetFolderIdType();

 

//Create the searchfolder under the Finder Folder

DistinguishedFolderIdType searchFolder = new DistinguishedFolderIdType();

searchFolder.Id = DistinguishedFolderIdNameType.searchfolders;

targetFolder.Item = searchFolder;

folderType.ParentFolderId = targetFolder;

//Create the search folder

CreateFolderResponseType createFolderResponse = esb.CreateFolder(folderType);

//Return the ID of the newly created search folder

FolderInfoResponseMessageType folderInfo = (FolderInfoResponseMessageType)createFolderResponse.ResponseMessages.Items[0];

return folderInfo.Folders.Length == 0 ? null : folderInfo.Folders[0].FolderId;