/// <summary>
        /// Log on to a mailbox with a specified user account and create a search folder.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="userPassword">Password of the user.</param>
        /// <param name="userDomain">Domain of the user.</param>
        /// <param name="searchFolderName">Name of the search folder.</param>
        /// <param name="searchText">Search text of the search folder.</param>
        /// <returns>If the search folder is created successfully, return true; otherwise, return false.</returns>
        public bool CreateSearchFolder(string userName, string userPassword, string userDomain, string searchFolderName, string searchText)
        {
            // Log on mailbox with specified user account(userName, userPassword, userDomain).
            bool isLoged = AdapterHelper.SwitchUser(userName, userPassword, userDomain, this.exchangeServiceBinding, this.Site);
            Site.Assert.IsTrue(
                isLoged,
                string.Format("Log on mailbox with the UserName: {0}, Password: {1}, Domain: {2} should be successful.", userName, userPassword, userDomain));

            // Create the request.
            CreateFolderType createFolder = new CreateFolderType();
            SearchFolderType[] folderArray = new SearchFolderType[1];
            SearchFolderType searchFolder = new SearchFolderType();

            // Use the following search filter to get all mail in the Inbox with the word searchText in the subject line.
            searchFolder.SearchParameters = new SearchParametersType();
            searchFolder.SearchParameters.Traversal = SearchFolderTraversalType.Deep;
            searchFolder.SearchParameters.TraversalSpecified = true;
            searchFolder.SearchParameters.BaseFolderIds = new DistinguishedFolderIdType[4];

            // Create a distinguished folder Identified of the inbox folder.
            DistinguishedFolderIdType inboxFolder = new DistinguishedFolderIdType();
            inboxFolder.Id = new DistinguishedFolderIdNameType();
            inboxFolder.Id = DistinguishedFolderIdNameType.inbox;
            searchFolder.SearchParameters.BaseFolderIds[0] = inboxFolder;
            DistinguishedFolderIdType contactType = new DistinguishedFolderIdType();
            contactType.Id = new DistinguishedFolderIdNameType();
            contactType.Id = DistinguishedFolderIdNameType.contacts;
            searchFolder.SearchParameters.BaseFolderIds[1] = contactType;
            DistinguishedFolderIdType calendarType = new DistinguishedFolderIdType();
            calendarType.Id = new DistinguishedFolderIdNameType();
            calendarType.Id = DistinguishedFolderIdNameType.calendar;
            searchFolder.SearchParameters.BaseFolderIds[2] = calendarType;
            DistinguishedFolderIdType taskType = new DistinguishedFolderIdType();
            taskType.Id = new DistinguishedFolderIdNameType();
            taskType.Id = DistinguishedFolderIdNameType.calendar;
            searchFolder.SearchParameters.BaseFolderIds[3] = taskType;

            // Use the following search filter.
            searchFolder.SearchParameters.Restriction = new RestrictionType();
            PathToUnindexedFieldType path = new PathToUnindexedFieldType();
            path.FieldURI = UnindexedFieldURIType.itemSubject;
            RestrictionType restriction = new RestrictionType();
            FieldURIOrConstantType fieldURIORConstant = new FieldURIOrConstantType();
            fieldURIORConstant.Item = new ConstantValueType();
            (fieldURIORConstant.Item as ConstantValueType).Value = searchText;
            ExistsType isEqual = new ExistsType();
            isEqual.Item = path;
            restriction.Item = isEqual;
            searchFolder.SearchParameters.Restriction = restriction;

            // Give the search folder a unique name.
            searchFolder.DisplayName = searchFolderName;
            folderArray[0] = searchFolder;

            // Create the search folder under the default Search Folder.
            TargetFolderIdType targetFolder = new TargetFolderIdType();
            DistinguishedFolderIdType searchFolders = new DistinguishedFolderIdType();
            searchFolders.Id = DistinguishedFolderIdNameType.searchfolders;
            targetFolder.Item = searchFolders;
            createFolder.ParentFolderId = targetFolder;
            createFolder.Folders = folderArray;
            bool isSearchFolderCreated = false;

            // Invoke CreateFolder operation and get the response.
            CreateFolderResponseType response = this.exchangeServiceBinding.CreateFolder(createFolder);
            if (response != null && ResponseClassType.Success == response.ResponseMessages.Items[0].ResponseClass)
            {
                // If the search folder is created successfully, return true; otherwise, return false.
                isSearchFolderCreated = true;

                searchFolder.FolderId = ((FolderInfoResponseMessageType)response.ResponseMessages.Items[0]).Folders[0].FolderId;
                AdapterHelper.CreatedFolders.Add(searchFolder);
            }

            return isSearchFolderCreated;
        }
        /// <summary>
        /// The method searches the mailbox and returns the items that meet a specified search restriction.
        /// </summary>
        /// <param name="folder">A string that specifies the folder to search.</param>
        /// <param name="value">A string that specifies the value for a search restriction.</param>
        /// <returns>If the method succeeds, return an array of item; otherwise, return null.</returns>
        private ItemIdType[] GetItemIds(string folder, string value)
        {
            #region Construct FindItem request
            FindItemType findRequest = new FindItemType();

            if (string.IsNullOrEmpty(folder) || string.IsNullOrEmpty(value))
            {
                Site.Assert.Fail("Invalid argument: one or more invalid arguments passed to GetItemIds method.");
            }

            findRequest.ItemShape = new ItemResponseShapeType();
            findRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;

            DistinguishedFolderIdType folderId = new DistinguishedFolderIdType();
            DistinguishedFolderIdNameType folderIdName;
            if (Enum.TryParse<DistinguishedFolderIdNameType>(folder, out folderIdName))
            {
                folderId.Id = folderIdName;
            }
            else
            {
                Site.Assert.Fail("The value of the first argument (foldIdNameType) of FindItem operation is invalid.");
            }

            findRequest.ParentFolderIds = new BaseFolderIdType[1];
            findRequest.ParentFolderIds[0] = folderId;

            PathToUnindexedFieldType itemClass = new PathToUnindexedFieldType();
            itemClass.FieldURI = UnindexedFieldURIType.itemSubject;
            ContainsExpressionType expressionType = new ContainsExpressionType();
            expressionType.Item = itemClass;
            expressionType.ContainmentMode = ContainmentModeType.Substring;
            expressionType.ContainmentModeSpecified = true;
            expressionType.ContainmentComparison = ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;
            expressionType.ContainmentComparisonSpecified = true;
            expressionType.Constant = new ConstantValueType();
            expressionType.Constant.Value = value;

            RestrictionType restriction = new RestrictionType();
            restriction.Item = expressionType;

            findRequest.Restriction = restriction;
            #endregion

            #region Get the ids of all ItemId instances
            int counter = 0;
            int upperBound = int.Parse(Common.GetConfigurationPropertyValue("RetryCount", this.Site));
            int waitTime = int.Parse(Common.GetConfigurationPropertyValue("WaitTime", this.Site));
            FindItemResponseType findResponse = new FindItemResponseType();

            while (counter < upperBound)
            {
                Thread.Sleep(waitTime);

                findResponse = this.exchangeServiceBinding.FindItem(findRequest);
                if (findResponse != null
                    && findResponse.ResponseMessages != null
                    && findResponse.ResponseMessages.Items != null
                    && findResponse.ResponseMessages.Items.Length > 0)
                {
                    ArrayOfRealItemsType items = ((FindItemResponseMessageType)findResponse.ResponseMessages.Items[0]).RootFolder.Item as ArrayOfRealItemsType;

                    if (items.Items != null && items.Items.Length > 0)
                    {
                        List<ItemIdType> itemIds = new List<ItemIdType>();
                        foreach (ItemType item in items.Items)
                        {
                            if (item.ItemId != null)
                            {
                                itemIds.Add(item.ItemId);
                            }
                        }

                        if (itemIds.Count > 0)
                        {
                            return itemIds.ToArray();
                        }
                    }
                }

                counter++;
            }

            Site.Log.Add(LogEntryKind.Debug, "When there is not any message found by FindItem operation, the retry count is {0}", counter);
            return null;
            #endregion
        }
        /// <summary>
        /// The operation construct a request for FindItem operation.
        /// </summary>
        /// <param name="folderName">A string that specifies the folder to search.</param>
        /// <param name="value">A string that specifies the value for a search restriction.</param>
        /// <param name="field">A string that specifies the type of referenced field URI.</param>
        /// <returns>The request of FindItem operation which constructed with special folder name, search restriction and referenced field URI</returns>
        protected FindItemType ConstructFindItemRequest(string folderName, string value, string field)
        {
            FindItemType findRequest = new FindItemType();
            findRequest.ItemShape = new ItemResponseShapeType();
            findRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;

            DistinguishedFolderIdType folderId = new DistinguishedFolderIdType();
            DistinguishedFolderIdNameType folderIdName;
            if (Enum.TryParse<DistinguishedFolderIdNameType>(folderName, out folderIdName))
            {
                folderId.Id = folderIdName;
            }
            else
            {
                Site.Assert.Fail("The value of the first argument (foldIdNameType) of FindItem operation is invalid.");
            }

            findRequest.ParentFolderIds = new BaseFolderIdType[1];
            findRequest.ParentFolderIds[0] = folderId;

            PathToUnindexedFieldType itemClass = new PathToUnindexedFieldType();
            UnindexedFieldURIType fieldURI;
            if (Enum.TryParse<UnindexedFieldURIType>(field, out fieldURI))
            {
                // set search field.
                itemClass.FieldURI = fieldURI;
            }
            else
            {
                Site.Assert.Fail("The value of the second argument (fieldURIType) of FindItem operation is invalid.");
            }

            ContainsExpressionType expressionType = new ContainsExpressionType();
            expressionType.Item = itemClass;
            expressionType.ContainmentMode = ContainmentModeType.Substring;
            expressionType.ContainmentModeSpecified = true;
            expressionType.ContainmentComparison = ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;
            expressionType.ContainmentComparisonSpecified = true;
            expressionType.Constant = new ConstantValueType();
            expressionType.Constant.Value = value;

            RestrictionType restriction = new RestrictionType();
            restriction.Item = expressionType;

            if (!string.IsNullOrEmpty(value))
            {
                findRequest.Restriction = restriction;
            }

            return findRequest;
        }
        /// <summary>
        /// Set related folder properties of create folder request
        /// </summary>
        /// <param name="displayNames">Display names of folders that will be set into create folder request.</param>
        /// <param name="folderClasses">Folder class values of folders that will be set into create folder request.</param>
        /// <param name="folderPermissions">Folder permission values of folders that will be set into create folder request. </param>
        /// <param name="createFolderRequest">Create folder request instance that needs to set property values.</param>
        /// <returns>Create folder request instance that have folder property value configured.</returns>
        protected CreateFolderType ConfigureFolderProperty(string[] displayNames, string[] folderClasses, PermissionSetType[] folderPermissions, CreateFolderType createFolderRequest)
        {
            Site.Assert.IsNotNull(displayNames, "Display names should not be null!");
            Site.Assert.IsNotNull(folderClasses, "Folder classes should not be null!");
            Site.Assert.AreEqual<int>(displayNames.Length, folderClasses.Length, "Folder names count should equals to folder class value count!");
            if (folderPermissions != null)
            {
                Site.Assert.AreEqual<int>(displayNames.Length, folderPermissions.Length, "Folder names count should equals to folder permission value count!");
            }

            int folderCount = displayNames.Length;
            createFolderRequest.Folders = new BaseFolderType[folderCount];
            for (int folderPropertyIndex = 0; folderPropertyIndex < folderCount; folderPropertyIndex++)
            {
                string folderResourceName = Common.GenerateResourceName(this.Site, displayNames[folderPropertyIndex]);

                if (folderClasses[folderPropertyIndex] == "IPF.Appointment")
                {
                    CalendarFolderType calendarFolder = new CalendarFolderType();
                    calendarFolder.DisplayName = folderResourceName;
                    createFolderRequest.Folders[folderPropertyIndex] = calendarFolder;
                }
                else if (folderClasses[folderPropertyIndex] == "IPF.Contact")
                {
                    ContactsFolderType contactFolder = new ContactsFolderType();
                    contactFolder.DisplayName = folderResourceName;
                    if (folderPermissions != null)
                    {
                        contactFolder.PermissionSet = folderPermissions[folderPropertyIndex];
                    }

                    createFolderRequest.Folders[folderPropertyIndex] = contactFolder;
                }
                else if (folderClasses[folderPropertyIndex] == "IPF.Task")
                {
                    TasksFolderType taskFolder = new TasksFolderType();
                    taskFolder.DisplayName = folderResourceName;
                    if (folderPermissions != null)
                    {
                        taskFolder.PermissionSet = folderPermissions[folderPropertyIndex];
                    }

                    createFolderRequest.Folders[folderPropertyIndex] = taskFolder;
                }
                else if (folderClasses[folderPropertyIndex] == "IPF.Search")
                {
                    SearchFolderType searchFolder = new SearchFolderType();
                    searchFolder.DisplayName = folderResourceName;

                    // Set search parameters.
                    searchFolder.SearchParameters = new SearchParametersType();
                    searchFolder.SearchParameters.Traversal = SearchFolderTraversalType.Deep;
                    searchFolder.SearchParameters.TraversalSpecified = true;
                    searchFolder.SearchParameters.BaseFolderIds = new DistinguishedFolderIdType[1];
                    DistinguishedFolderIdType inboxType = new DistinguishedFolderIdType();
                    inboxType.Id = new DistinguishedFolderIdNameType();
                    inboxType.Id = DistinguishedFolderIdNameType.inbox;
                    searchFolder.SearchParameters.BaseFolderIds[0] = inboxType;

                    // Use the following search filter 
                    searchFolder.SearchParameters.Restriction = new RestrictionType();
                    PathToUnindexedFieldType path = new PathToUnindexedFieldType();
                    path.FieldURI = UnindexedFieldURIType.itemSubject;
                    RestrictionType restriction = new RestrictionType();
                    ExistsType isEqual = new ExistsType();
                    isEqual.Item = path;
                    restriction.Item = isEqual;
                    searchFolder.SearchParameters.Restriction = restriction;

                    if (folderPermissions != null)
                    {
                        searchFolder.PermissionSet = folderPermissions[folderPropertyIndex];
                    }

                    createFolderRequest.Folders[folderPropertyIndex] = searchFolder;
                }
                else
                {
                    // Set Display Name and Folder Class for the folder to be created.
                    FolderType folder = new FolderType();
                    folder.DisplayName = folderResourceName;
                    folder.FolderClass = folderClasses[folderPropertyIndex];

                    if (folderPermissions != null)
                    {
                        folder.PermissionSet = folderPermissions[folderPropertyIndex];
                    }

                    createFolderRequest.Folders[folderPropertyIndex] = folder;
                }
            }

            return createFolderRequest;
        }
        public void MSOXWSCORE_S01_TC24_CreateItemAssociatedWithFolder()
        {
            Site.Assume.IsTrue(Common.IsRequirementEnabled(2285, this.Site), "Exchange 2007 does not support the IsAssociated element.");

            #region Create an user configuration object.
            // User configuration objects are items that are associated with folders in a mailbox.
            string userConfiguratioName = Common.GenerateResourceName(this.Site, "UserConfigurationSampleName").Replace("_", string.Empty);
            bool isSuccess = this.USRCFGSUTControlAdapter.CreateUserConfiguration(
                Common.GetConfigurationPropertyValue("User1Name", this.Site),
                Common.GetConfigurationPropertyValue("User1Password", this.Site),
                Common.GetConfigurationPropertyValue("Domain", this.Site),
                userConfiguratioName);
            Site.Assert.IsTrue(isSuccess, "The user configuration object should be created successfully.");
            #endregion

            #region Find the created user configuration object
            FindItemType findRequest = new FindItemType();
            findRequest.ItemShape = new ItemResponseShapeType();
            findRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
            findRequest.ParentFolderIds = new BaseFolderIdType[1]
            {
                new DistinguishedFolderIdType() { Id = DistinguishedFolderIdNameType.inbox }
            };
            PathToUnindexedFieldType itemSubject = new PathToUnindexedFieldType();
            itemSubject.FieldURI = UnindexedFieldURIType.itemItemClass;
            ContainsExpressionType expressionType = new ContainsExpressionType();
            expressionType.Item = itemSubject;
            expressionType.ContainmentMode = ContainmentModeType.Substring;
            expressionType.ContainmentModeSpecified = true;
            expressionType.ContainmentComparison = ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;
            expressionType.ContainmentComparisonSpecified = true;
            expressionType.Constant = new ConstantValueType();
            expressionType.Constant.Value = "IPM.Configuration";

            RestrictionType restriction = new RestrictionType();
            restriction.Item = expressionType;
            findRequest.Restriction = restriction;
            findRequest.Traversal = ItemQueryTraversalType.Associated;

            FindItemResponseType findResponse = this.SRCHAdapter.FindItem(findRequest);
            ItemType[] foundItems = (((FindItemResponseMessageType)findResponse.ResponseMessages.Items[0]).RootFolder.Item as ArrayOfRealItemsType).Items;
            ItemType item = null;
            foreach (ItemType foundItem in foundItems)
            {
                if (foundItem.ItemClass.Contains(userConfiguratioName))
                {
                    item = foundItem;
                    break;
                }
            }

            Site.Assert.IsNotNull(item, "The created user configuration object should be found!");

            // Add the debug information
            this.Site.Log.Add(LogEntryKind.Debug, "Verify MS-OXWSCORE_R1618");

            // Verify MS-OXWSCORE requirement: MS-OXWSCORE_R1618
            this.Site.CaptureRequirementIfIsTrue(
                item.IsAssociatedSpecified && item.IsAssociated,
                1618,
                @"[In t:ItemType Complex Type] [IsAssociated is] True, indicates the item is associated with a folder.");
            #endregion
        }