HOW TO: Create SearchFolder to search for Custom Properties using EWS


Here is a quick sample if you are looking at creating a search folder using EWS which will look for Custom Property.

Here we are looking for a custom property named “EmployeeID” and make sure that the property exist and does not have a blank value.

public static BaseFolderType CreateCustomPropSearchFolder()
       {
           
           //Base folder type
           FolderIdType folderID = new FolderIdType();

           //create the request that will create a new searchfolder under the finder directory
           CreateFolderType folderType = new CreateFolderType();

           // inherited type from FolderType
           SearchFolderType[] folderArray = new SearchFolderType[1];

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

           // Go a Deep traversal and search every folder inside parent folder
           folderArray[0].SearchParameters.Traversal = SearchFolderTraversalType.Deep;        // deep traversal
           folderArray[0].SearchParameters.TraversalSpecified = true;                        // must set it to true otherwise traversal will have no effect


           //Define the base folder to search under, in this case its Inbox
           folderArray[0].SearchParameters.BaseFolderIds = new DistinguishedFolderIdType[1];
           DistinguishedFolderIdType dType = new DistinguishedFolderIdType();
           dType.Id = new DistinguishedFolderIdNameType();
           dType.Id = DistinguishedFolderIdNameType.inbox;                                    //  we are looking at Inbox only
           folderArray[0].SearchParameters.BaseFolderIds[0] = dType;


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


           PathToExtendedFieldType CustomPropPath = new PathToExtendedFieldType();
           CustomPropPath.DistinguishedPropertySetId = DistinguishedPropertySetType.PublicStrings;
           CustomPropPath.DistinguishedPropertySetIdSpecified = true;
           CustomPropPath.PropertyName = "EmployeeID";
           CustomPropPath.PropertyType = MapiPropertyTypeType.String;

           ExistsType existsType = new ExistsType();
           existsType.Item = CustomPropPath;
           

           // First part where the 
           IsNotEqualToType isNotEqualType = new IsNotEqualToType();
           isNotEqualType.FieldURIOrConstant = new FieldURIOrConstantType();

           //Actual value of property that we are looking at
           ConstantValueType customPropertyValue = new ConstantValueType();
           customPropertyValue.Value = string.Empty;

           isNotEqualType.FieldURIOrConstant.Item = customPropertyValue;

           
           isNotEqualType.Item = CustomPropPath;
           

           // Create a AND EXPRESSION & add two condition to it
           AndType andTypeExpressions = new AndType();
           andTypeExpressions.Items = new SearchExpressionType[] {isNotEqualType,existsType };
           

           folderArray[0].SearchParameters.Restriction.Item = andTypeExpressions;
           folderArray[0].DisplayName = "Employee Search";                        // Give your search folder a unique name
           folderType.Folders = folderArray;

           TargetFolderIdType targetFolder = new TargetFolderIdType();

           //Create the searchfolder under the Finder Folder

           DistinguishedFolderIdType searchFolder = new DistinguishedFolderIdType();

           searchFolder.Id = DistinguishedFolderIdNameType.searchfolders;                    // Saving it under searchfolders root

           targetFolder.Item = searchFolder;

           folderType.ParentFolderId = targetFolder;

           // Uncomment the following lines of code to make it a hidden search folder, can be consumed by other programs and not visible to users


           // folderArray[0].ExtendedProperty = new ExtendedPropertyType[1];
           // folderArray[0].ExtendedProperty[0] = new ExtendedPropertyType();
           // folderArray[0].ExtendedProperty[0].ExtendedFieldURI = new PathToExtendedFieldType();
           // folderArray[0].ExtendedProperty[0].ExtendedFieldURI.PropertyTag="0x10F4";   //PR_ATTR_HIDDEN
           // folderArray[0].ExtendedProperty[0].ExtendedFieldURI.PropertyType = MapiPropertyTypeType.Boolean;
           // folderArray[0].ExtendedProperty[0].Item = "true";



           //Create the search folder
           CreateFolderResponseType createFolderResponse = esb.CreateFolder(folderType);
           //Return the newly created search folder

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

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

       }
Skip to main content