Inheritance: BaseFolderIdType
        public void MSOXWSFOLD_S08_TC01_AllOperationsWithAllOptionalElements()
        {
            #region Configure SOAP header

            this.ConfigureSOAPHeader();

            #endregion

            #region Create new folders in the inbox folder.

            // CreateFolder request.
            CreateFolderType createFolderRequest = this.GetCreateFolderRequest(
                DistinguishedFolderIdNameType.inbox.ToString(),
                new string[] { "Custom Folder1", "Custom Folder2", "Custom Folder3", "Custom Folder4" },
                new string[] { "IPF.MyCustomFolderClass", "IPF.Appointment", "IPF.Contact", "IPF.Task" },
                null);

            // Set ExtendedProperty defined in BaseFolderType.
            PathToExtendedFieldType publishInAddressBook = new PathToExtendedFieldType();

            // A hexadecimal tag of the extended property.
            publishInAddressBook.PropertyTag = "0x671E";
            publishInAddressBook.PropertyType = MapiPropertyTypeType.Boolean;
            ExtendedPropertyType pubAddressbook = new ExtendedPropertyType();
            pubAddressbook.ExtendedFieldURI = publishInAddressBook;
            pubAddressbook.Item = "1";
            ExtendedPropertyType[] extendedProperties = new ExtendedPropertyType[1];
            extendedProperties[0] = pubAddressbook;

            createFolderRequest.Folders[0].ExtendedProperty = extendedProperties;
            createFolderRequest.Folders[1].ExtendedProperty = extendedProperties;
            createFolderRequest.Folders[2].ExtendedProperty = extendedProperties;
            createFolderRequest.Folders[3].ExtendedProperty = extendedProperties;

            // Define a permissionSet with all optional elements
            PermissionSetType permissionSet = new PermissionSetType();
            permissionSet.Permissions = new PermissionType[1];
            permissionSet.Permissions[0] = new PermissionType();
            permissionSet.Permissions[0].ReadItems = new PermissionReadAccessType();
            permissionSet.Permissions[0].ReadItems = PermissionReadAccessType.FullDetails;
            permissionSet.Permissions[0].ReadItemsSpecified = true;
            permissionSet.Permissions[0].CanCreateItems = true;
            permissionSet.Permissions[0].CanCreateItemsSpecified = true;
            permissionSet.Permissions[0].CanCreateSubFolders = true;
            permissionSet.Permissions[0].CanCreateSubFoldersSpecified = true;
            permissionSet.Permissions[0].IsFolderVisible = true;
            permissionSet.Permissions[0].IsFolderVisibleSpecified = true;
            permissionSet.Permissions[0].IsFolderContact = true;
            permissionSet.Permissions[0].IsFolderContactSpecified = true;
            permissionSet.Permissions[0].IsFolderOwner = true;
            permissionSet.Permissions[0].IsFolderOwnerSpecified = true;
            permissionSet.Permissions[0].IsFolderContact = true;
            permissionSet.Permissions[0].IsFolderContactSpecified = true;
            permissionSet.Permissions[0].EditItems = new PermissionActionType();
            permissionSet.Permissions[0].EditItems = PermissionActionType.All;
            permissionSet.Permissions[0].EditItemsSpecified = true;
            permissionSet.Permissions[0].DeleteItems = new PermissionActionType();
            permissionSet.Permissions[0].DeleteItems = PermissionActionType.All;
            permissionSet.Permissions[0].DeleteItemsSpecified = true;
            permissionSet.Permissions[0].PermissionLevel = new PermissionLevelType();
            permissionSet.Permissions[0].PermissionLevel = PermissionLevelType.Custom;
            permissionSet.Permissions[0].UserId = new UserIdType();
            permissionSet.Permissions[0].UserId.PrimarySmtpAddress = Common.GetConfigurationPropertyValue("User2Name", this.Site) + "@" + Common.GetConfigurationPropertyValue("Domain", this.Site);

            // Set PermissionSet for FolderType folder.
            ((FolderType)createFolderRequest.Folders[0]).PermissionSet = permissionSet;

            // Set PermissionSet for ContactsType folder.
            ((ContactsFolderType)createFolderRequest.Folders[2]).PermissionSet = permissionSet;

            // Set PermissionSet for TasksFolderType folder.
            ((TasksFolderType)createFolderRequest.Folders[3]).PermissionSet = permissionSet;

            // Create a new folder.
            CreateFolderResponseType createFolderResponse = this.FOLDAdapter.CreateFolder(createFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(createFolderResponse, 4, this.Site);

            // Folder ids.
            FolderIdType[] folderIds = new FolderIdType[createFolderResponse.ResponseMessages.Items.Length];

            for (int index = 0; index < createFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, createFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be created successfully!");

                // Save folder ids.
                folderIds[index] = ((FolderInfoResponseMessageType)createFolderResponse.ResponseMessages.Items[index]).Folders[0].FolderId;

                // Save the new created folder's folder id.
                this.NewCreatedFolderIds.Add(folderIds[index]);
            }

            #endregion

            #region Create a managedfolder

            CreateManagedFolderRequestType createManagedFolderRequest = this.GetCreateManagedFolderRequest(Common.GetConfigurationPropertyValue("ManagedFolderName1", this.Site));

            // Add an email address into request.
            EmailAddressType mailBox = new EmailAddressType()
            {
                EmailAddress = Common.GetConfigurationPropertyValue("User1Name", this.Site) + "@" + Common.GetConfigurationPropertyValue("Domain", this.Site)
            };

            createManagedFolderRequest.Mailbox = mailBox;

            // Create the specified managed folder.
            CreateManagedFolderResponseType createManagedFolderResponse = this.FOLDAdapter.CreateManagedFolder(createManagedFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(createManagedFolderResponse, 1, this.Site);

            // Save the new created managed folder's folder id.
            FolderIdType newFolderId = ((FolderInfoResponseMessageType)createManagedFolderResponse.ResponseMessages.Items[0]).Folders[0].FolderId;
            this.NewCreatedFolderIds.Add(newFolderId);

            #endregion

            #region Get the new created folders

            // GetFolder request.
            GetFolderType getCreatedFolderRequest = this.GetGetFolderRequest(DefaultShapeNamesType.AllProperties, folderIds);

            // Get the new created folder.
            GetFolderResponseType getCreatedFolderResponse = this.FOLDAdapter.GetFolder(getCreatedFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(getCreatedFolderResponse, 4, this.Site);

            for (int index = 0; index < getCreatedFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, getCreatedFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder information should be returned!");
            }

            #endregion

            #region Update the new created folders

            // UpdateFolder request.
            UpdateFolderType updateFolderRequest = this.GetUpdateFolderRequest(
                new string[] { "Folder", "CalendarFolder", "ContactsFolder", "TasksFolder" },
                new string[] { "SetFolderField", "SetFolderField", "SetFolderField", "SetFolderField" },
                folderIds);

            // Update the folders' properties.
            UpdateFolderResponseType updateFolderResponse = this.FOLDAdapter.UpdateFolder(updateFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(updateFolderResponse, 4, this.Site);

            for (int index = 0; index < updateFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, updateFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be updated successfully!");
            }

            #endregion

            #region Copy the updated folders to "drafts" folder

            // Copy the folders into "drafts" folder
            CopyFolderType copyFolderRequest = this.GetCopyFolderRequest(DistinguishedFolderIdNameType.drafts.ToString(), folderIds);

            // Copy the folders.
            CopyFolderResponseType copyFolderResponse = this.FOLDAdapter.CopyFolder(copyFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(copyFolderResponse, 4, this.Site);

            // Copied Folders' id.
            FolderIdType[] copiedFolderIds = new FolderIdType[copyFolderResponse.ResponseMessages.Items.Length];

            for (int index = 0; index < copyFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, copyFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be updated successfully!");

                // Variable to save the folders.
                copiedFolderIds[index] = ((FolderInfoResponseMessageType)copyFolderResponse.ResponseMessages.Items[index]).Folders[0].FolderId;

                // Save the copied folders' folder id.
                this.NewCreatedFolderIds.Add(copiedFolderIds[index]);
            }

            #endregion

            #region Move the updated folders to "deleteditems" folder

            // MoveFolder request.
            MoveFolderType moveFolderRequest = new MoveFolderType();

            // Set the request's folderId field.
            moveFolderRequest.FolderIds = folderIds;

            // Set the request's destFolderId field.
            DistinguishedFolderIdType toFolderId = new DistinguishedFolderIdType();
            toFolderId.Id = DistinguishedFolderIdNameType.deleteditems;
            moveFolderRequest.ToFolderId = new TargetFolderIdType();
            moveFolderRequest.ToFolderId.Item = toFolderId;

            // Move the specified folders.
            MoveFolderResponseType moveFolderResponse = this.FOLDAdapter.MoveFolder(moveFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(moveFolderResponse, 4, this.Site);

            for (int index = 0; index < moveFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, moveFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be updated successfully!");
            }
            #endregion

            #region Delete all folders

            // All folder ids.
            FolderIdType[] allFolderIds = new FolderIdType[folderIds.Length + copiedFolderIds.Length];

            for (int index = 0; index < allFolderIds.Length / 2; index++)
            {
                allFolderIds[index] = folderIds[index];
                allFolderIds[index + folderIds.Length] = copiedFolderIds[index];
            }

            // DeleteFolder request.
            DeleteFolderType deleteFolderRequest = this.GetDeleteFolderRequest(DisposalType.HardDelete, allFolderIds);

            // Delete the specified folder.
            DeleteFolderResponseType deleteFolderResponse = this.FOLDAdapter.DeleteFolder(deleteFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(deleteFolderResponse, 8, this.Site);

            for (int index = 0; index < deleteFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, deleteFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be updated successfully!");
            }

            #endregion
        }
 /// <summary>
 /// Generate the request message for operation "UpdateFolder".
 /// </summary>
 /// <param name="folderType">Identifies type of the folder.</param>
 /// <param name="updateType">Identifies update type of the folder.</param>
 /// <param name="folderId">The folder identifier of the folder need to be updated.</param>
 /// <returns>Update folder request instance that will send to server.</returns>
 protected UpdateFolderType GetUpdateFolderRequest(string folderType, string updateType, FolderIdType folderId)
 {
     UpdateFolderType updateFolderRequest = this.GetUpdateFolderRequest(new string[] { folderType }, new string[] { updateType }, new FolderIdType[] { folderId });
     return updateFolderRequest;
 }
        public void MSOXWSFOLD_S08_TC02_AllOperationsWithoutAllOptionalElements()
        {
            #region Create new folders in the inbox folder.

            // CreateFolder request.
            CreateFolderType createFolderRequest = this.GetCreateFolderRequest(
                DistinguishedFolderIdNameType.inbox.ToString(),
                new string[] { "Custom Folder1", "Custom Folder2", "Custom Folder3", "Custom Folder4" },
                new string[] { "IPF.MyCustomFolderClass", "IPF.Appointment", "IPF.Contact", "IPF.Task" },
                null);

            // Remove FolderClass for FolderType folder.
            createFolderRequest.Folders[0].FolderClass = null;

            // Create a new folder.
            CreateFolderResponseType createFolderResponse = this.FOLDAdapter.CreateFolder(createFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(createFolderResponse, 4, this.Site);

            // Folder ids.
            FolderIdType[] folderIds = new FolderIdType[createFolderResponse.ResponseMessages.Items.Length];

            for (int index = 0; index < createFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, createFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be created successfully!");

                // Save folder ids.
                folderIds[index] = ((FolderInfoResponseMessageType)createFolderResponse.ResponseMessages.Items[index]).Folders[0].FolderId;

                // Save the new created folder's folder id.
                this.NewCreatedFolderIds.Add(folderIds[index]);
            }

            #endregion

            #region Create a managedfolder

            CreateManagedFolderRequestType createManagedFolderRequest = this.GetCreateManagedFolderRequest(Common.GetConfigurationPropertyValue("ManagedFolderName1", this.Site));

            // Create the specified managed folder.
            CreateManagedFolderResponseType createManagedFolderResponse = this.FOLDAdapter.CreateManagedFolder(createManagedFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(createManagedFolderResponse, 1, this.Site);

            // Save the new created managed folder's folder id.
            FolderIdType newManagedFolderId = ((FolderInfoResponseMessageType)createManagedFolderResponse.ResponseMessages.Items[0]).Folders[0].FolderId;
            this.NewCreatedFolderIds.Add(newManagedFolderId);

            #endregion

            #region Get the new created folders

            // GetFolder request.
            GetFolderType getCreatedFolderRequest = this.GetGetFolderRequest(DefaultShapeNamesType.AllProperties, folderIds);

            // Get the new created folder.
            GetFolderResponseType getCreatedFolderResponse = this.FOLDAdapter.GetFolder(getCreatedFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(getCreatedFolderResponse, 4, this.Site);

            for (int index = 0; index < getCreatedFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, getCreatedFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder information should be returned!");
            }

            #endregion

            #region Update the new created folders

            // UpdateFolder request.
            UpdateFolderType updateFolderRequest = this.GetUpdateFolderRequest(
                new string[] { "Folder", "CalendarFolder", "ContactsFolder", "TasksFolder" },
                new string[] { "SetFolderField", "SetFolderField", "SetFolderField", "SetFolderField" },
                folderIds);

            // Update the folders' properties.
            UpdateFolderResponseType updateFolderResponse = this.FOLDAdapter.UpdateFolder(updateFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(updateFolderResponse, 4, this.Site);

            for (int index = 0; index < updateFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, updateFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be updated successfully!");
            }

            #endregion

            #region Copy the updated folders to "drafts" folder

            // Copy the folders into "drafts" folder
            CopyFolderType copyFolderRequest = this.GetCopyFolderRequest(DistinguishedFolderIdNameType.drafts.ToString(), folderIds);

            // Copy the folders.
            CopyFolderResponseType copyFolderResponse = this.FOLDAdapter.CopyFolder(copyFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(copyFolderResponse, 4, this.Site);

            // Copied folders' id.
            FolderIdType[] copiedFolderIds = new FolderIdType[copyFolderResponse.ResponseMessages.Items.Length];

            for (int index = 0; index < copyFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, copyFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be updated successfully!");

                // Variable to save the folders.
                copiedFolderIds[index] = ((FolderInfoResponseMessageType)copyFolderResponse.ResponseMessages.Items[index]).Folders[0].FolderId;

                // Save the copied folders' folder id.
                this.NewCreatedFolderIds.Add(copiedFolderIds[index]);
            }

            #endregion

            #region Move the updated folders to "deleteditems" folder

            // MoveFolder request.
            MoveFolderType moveFolderRequest = new MoveFolderType();

            // Set the request's folderId field.
            moveFolderRequest.FolderIds = folderIds;

            // Set the request's destFolderId field.
            DistinguishedFolderIdType toFolderId = new DistinguishedFolderIdType();
            toFolderId.Id = DistinguishedFolderIdNameType.deleteditems;
            moveFolderRequest.ToFolderId = new TargetFolderIdType();
            moveFolderRequest.ToFolderId.Item = toFolderId;

            // Move the specified folders.
            MoveFolderResponseType moveFolderResponse = this.FOLDAdapter.MoveFolder(moveFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(moveFolderResponse, 4, this.Site);

            for (int index = 0; index < moveFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, moveFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be updated successfully!");
            }

            #endregion

            #region Delete all folders

            // All folder ids.
            FolderIdType[] allFolderIds = new FolderIdType[folderIds.Length + copiedFolderIds.Length];

            for (int index = 0; index < allFolderIds.Length / 2; index++)
            {
                allFolderIds[index] = folderIds[index];
                allFolderIds[index + folderIds.Length] = copiedFolderIds[index];
            }

            // DeleteFolder request.
            DeleteFolderType deleteFolderRequest = this.GetDeleteFolderRequest(DisposalType.HardDelete, allFolderIds);

            // Delete the specified folder.
            DeleteFolderResponseType deleteFolderResponse = this.FOLDAdapter.DeleteFolder(deleteFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(deleteFolderResponse, 8, this.Site);

            for (int index = 0; index < deleteFolderResponse.ResponseMessages.Items.Length; index++)
            {
                Site.Assert.AreEqual<ResponseClassType>(ResponseClassType.Success, deleteFolderResponse.ResponseMessages.Items[index].ResponseClass, "Folder should be updated successfully!");
            }

            #endregion
        }
        /// <summary>
        /// Generate the request message for operation "CopyFolder".
        /// </summary>
        /// <param name="toFolderId">A target folder for operations that copy folders.</param>
        /// <param name="folderIds">An array of folder identifier of the folders need to be copied.</param>
        /// <returns>Copy folder request instance that will send to server.</returns>
        protected CopyFolderType GetCopyFolderRequest(string toFolderId, params BaseFolderIdType[] folderIds)
        {
            Site.Assert.IsNotNull(folderIds, "Folders id should not be null!");
            Site.Assert.AreNotEqual<int>(0, folderIds.Length, "Folders id should contains at least one id!");

            // CopyFolder request.
            CopyFolderType copyFolderRequest = new CopyFolderType();
            int folderCount = folderIds.Length;

            // Identify the folders to be copied.
            copyFolderRequest.FolderIds = new BaseFolderIdType[folderCount];
            for (int folderIdIndex = 0; folderIdIndex < folderCount; folderIdIndex++)
            {
                copyFolderRequest.FolderIds[folderIdIndex] = folderIds[folderIdIndex];
            }

            // Identify the destination folder.
            copyFolderRequest.ToFolderId = new TargetFolderIdType();

            DistinguishedFolderIdType distinguishedFolderId = new DistinguishedFolderIdType();
            DistinguishedFolderIdNameType distinguishedFolderIdName = new DistinguishedFolderIdNameType();
            bool isSuccess = Enum.TryParse<DistinguishedFolderIdNameType>(toFolderId, true, out distinguishedFolderIdName);

            if (isSuccess)
            {
                distinguishedFolderId.Id = distinguishedFolderIdName;
                copyFolderRequest.ToFolderId.Item = distinguishedFolderId;
            }
            else
            {
                FolderIdType id = new FolderIdType();
                id.Id = toFolderId;
                copyFolderRequest.ToFolderId.Item = id;
            }

            return copyFolderRequest;
        }
        /// <summary>
        /// Generate the request message for operation "UpdateFolder".
        /// </summary>
        /// <param name="folderType">An array of folder types.</param>
        /// <param name="updateType">An array of update folder types.</param>
        /// <param name="folderIds">An array of folder Ids.</param>
        /// <returns>Update folder request instance that will send to server.</returns>
        protected UpdateFolderType GetUpdateFolderRequest(string[] folderType, string[] updateType, FolderIdType[] folderIds)
        {
            Site.Assert.AreEqual<int>(folderType.Length, folderIds.Length, "Folder type count should equal to folder id count!");
            Site.Assert.AreEqual<int>(folderType.Length, updateType.Length, "Folder type count should equal to update type count!");

            // UpdateFolder request.
            UpdateFolderType updateFolderRequest = new UpdateFolderType();
            int folderCount = folderIds.Length;

            // Set the request's folder id field to Custom Folder's folder id.
            updateFolderRequest.FolderChanges = new FolderChangeType[folderCount];
            for (int folderIndex = 0; folderIndex < folderCount; folderIndex++)
            {
                updateFolderRequest.FolderChanges[folderIndex] = new FolderChangeType();
                updateFolderRequest.FolderChanges[folderIndex].Item = folderIds[folderIndex];

                // Add the array of changes; in this case, a single element array.
                updateFolderRequest.FolderChanges[folderIndex].Updates = new FolderChangeDescriptionType[1];

                switch (updateType[folderIndex])
                {
                    case "SetFolderField":
                        {
                            // Set the new folder name of the specific folder.
                            SetFolderFieldType setFolderField = new SetFolderFieldType();
                            PathToUnindexedFieldType displayNameProp = new PathToUnindexedFieldType();
                            displayNameProp.FieldURI = UnindexedFieldURIType.folderDisplayName;

                            switch (folderType[folderIndex])
                            {
                                case "Folder":
                                    FolderType updatedFolder = new FolderType();
                                    updatedFolder.DisplayName = Common.GenerateResourceName(this.Site, "UpdatedFolder" + folderIndex);
                                    setFolderField.Item1 = updatedFolder;
                                    break;
                                case "CalendarFolder":
                                    CalendarFolderType updatedCalendarFolder = new CalendarFolderType();
                                    updatedCalendarFolder.DisplayName = Common.GenerateResourceName(this.Site, "UpdatedFolder" + folderIndex);
                                    setFolderField.Item1 = updatedCalendarFolder;
                                    break;
                                case "ContactsFolder":
                                    CalendarFolderType updatedContactFolder = new CalendarFolderType();
                                    updatedContactFolder.DisplayName = Common.GenerateResourceName(this.Site, "UpdatedFolder" + folderIndex);
                                    setFolderField.Item1 = updatedContactFolder;
                                    break;
                                case "SearchFolder":
                                    CalendarFolderType updatedSearchFolder = new CalendarFolderType();
                                    updatedSearchFolder.DisplayName = Common.GenerateResourceName(this.Site, "UpdatedFolder" + folderIndex);
                                    setFolderField.Item1 = updatedSearchFolder;
                                    break;
                                case "TasksFolder":
                                    CalendarFolderType updatedTaskFolder = new CalendarFolderType();
                                    updatedTaskFolder.DisplayName = Common.GenerateResourceName(this.Site, "UpdatedFolder" + folderIndex);
                                    setFolderField.Item1 = updatedTaskFolder;
                                    break;

                                default:
                                    FolderType generalFolder = new FolderType();
                                    generalFolder.DisplayName = Common.GenerateResourceName(this.Site, "UpdatedFolder" + folderIndex);
                                    setFolderField.Item1 = generalFolder;
                                    break;
                            }

                            setFolderField.Item = displayNameProp;
                            updateFolderRequest.FolderChanges[folderIndex].Updates[0] = setFolderField;
                        }

                        break;
                    case "DeleteFolderField":
                        {
                            // Use DeleteFolderFieldType.
                            DeleteFolderFieldType delFolder = new DeleteFolderFieldType();
                            PathToUnindexedFieldType delProp = new PathToUnindexedFieldType();
                            delProp.FieldURI = UnindexedFieldURIType.folderPermissionSet;
                            delFolder.Item = delProp;
                            updateFolderRequest.FolderChanges[folderIndex].Updates[0] = delFolder;
                        }

                        break;
                    case "AppendToFolderField":
                        {
                            // Use AppendToFolderFieldType.
                            AppendToFolderFieldType appendToFolderField = new AppendToFolderFieldType();
                            PathToUnindexedFieldType displayNameAppendTo = new PathToUnindexedFieldType();
                            displayNameAppendTo.FieldURI = UnindexedFieldURIType.calendarAdjacentMeetings;
                            appendToFolderField.Item = displayNameAppendTo;
                            FolderType folderAppendTo = new FolderType();
                            folderAppendTo.FolderId = folderIds[folderIndex];
                            appendToFolderField.Item1 = folderAppendTo;
                            updateFolderRequest.FolderChanges[folderIndex].Updates[0] = appendToFolderField;
                        }

                        break;
                }
            }

            return updateFolderRequest;
        }
        /// <summary>
        /// Create item within a specific folder.
        /// </summary>
        /// <param name="toAddress">To address of created item</param>
        /// <param name="folderId">Parent folder id of the created item.</param>
        /// <param name="subject">Subject of the item.</param>
        /// <returns>Id of created item.</returns>
        protected ItemIdType CreateItem(string toAddress, string folderId, string subject)
        {
            CreateItemType createItemRequest = new CreateItemType();
            createItemRequest.MessageDispositionSpecified = true;
            createItemRequest.MessageDisposition = MessageDispositionType.SaveOnly;
            createItemRequest.SendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToAllAndSaveCopy;
            createItemRequest.SendMeetingInvitationsSpecified = true;
            createItemRequest.SavedItemFolderId = new TargetFolderIdType();

            DistinguishedFolderIdType distinguishedFolderId = new DistinguishedFolderIdType();
            DistinguishedFolderIdNameType distinguishedFolderIdName = new DistinguishedFolderIdNameType();
            bool isSuccess = Enum.TryParse<DistinguishedFolderIdNameType>(folderId, true, out distinguishedFolderIdName);

            if (isSuccess)
            {
                distinguishedFolderId.Id = distinguishedFolderIdName;
                createItemRequest.SavedItemFolderId.Item = distinguishedFolderId;
            }
            else
            {
                FolderIdType id = new FolderIdType();
                id.Id = folderId;
                createItemRequest.SavedItemFolderId.Item = id;
            }

            MessageType message = new MessageType();
            message.Subject = subject;
            EmailAddressType address = new EmailAddressType();
            address.EmailAddress = toAddress;

            // Set this message to unread.
            message.IsRead = false;
            message.IsReadSpecified = true;
            message.ToRecipients = new EmailAddressType[1];
            message.ToRecipients[0] = address;
            BodyType body = new BodyType();
            body.Value = Common.GenerateResourceName(this.Site, "Test Mail Body");
            message.Body = body;

            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items = new ItemType[1];
            createItemRequest.Items.Items[0] = message;
            CreateItemResponseType createItemResponse = this.COREAdapter.CreateItem(createItemRequest);
            ItemInfoResponseMessageType itemInfo = (ItemInfoResponseMessageType)createItemResponse.ResponseMessages.Items[0];

            // Return item id.
            if (itemInfo.Items.Items != null)
            {
                return itemInfo.Items.Items[0].ItemId;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// Generate the request message for operation "CreateFolder".
        /// </summary>
        /// <param name="parentFolderId">The folder identifier for the parent folder.</param>
        /// <param name="folderNames">An array of display name of the folders to be created.</param>
        /// <param name="folderClasses">An array of folder class value of the folders to be created.</param>
        /// <param name="permissionSet">An array of permission set value of the folder.</param>
        /// <returns>Create folder request instance that will send to server.</returns>
        protected CreateFolderType GetCreateFolderRequest(string parentFolderId, string[] folderNames, string[] folderClasses, PermissionSetType[] permissionSet)
        {
            CreateFolderType createFolderRequest = new CreateFolderType();
            createFolderRequest.ParentFolderId = new TargetFolderIdType();

            DistinguishedFolderIdType distinguishedFolderId = new DistinguishedFolderIdType();
            DistinguishedFolderIdNameType distinguishedFolderIdName = new DistinguishedFolderIdNameType();
            bool isSuccess = Enum.TryParse<DistinguishedFolderIdNameType>(parentFolderId, true, out distinguishedFolderIdName);

            if (isSuccess)
            {
                distinguishedFolderId.Id = distinguishedFolderIdName;
                createFolderRequest.ParentFolderId.Item = distinguishedFolderId;
            }
            else
            {
                FolderIdType id = new FolderIdType();
                id.Id = parentFolderId;
                createFolderRequest.ParentFolderId.Item = id;
            }

            createFolderRequest = this.ConfigureFolderProperty(folderNames, folderClasses, permissionSet, createFolderRequest);

            return createFolderRequest;
        }
        /// <summary>
        /// Create an item in the specified folder.
        /// </summary>
        /// <param name="parentFolderType">Type of the parent folder.</param>
        /// <param name="parentFolderId">ID of the parent folder.</param>
        /// <param name="itemSubject">Subject of the item which should be created.</param>
        /// <returns>ID of the created item.</returns>
        protected ItemIdType CreateItem(DistinguishedFolderIdNameType parentFolderType, string parentFolderId, string itemSubject)
        {
            // Create a request for the CreateItem operation and initialize the ItemType instance.
            CreateItemType createItemRequest = new CreateItemType();
            ItemType item = null;

            // Get different values for item based on different parent folder type.
            switch (parentFolderType)
            {
                case DistinguishedFolderIdNameType.contacts:
                    ContactItemType contact = new ContactItemType();
                    contact.Subject = itemSubject;
                    contact.FileAs = itemSubject;
                    item = contact;
                    break;
                case DistinguishedFolderIdNameType.calendar:
                    // Set the sendMeetingInvitations property.
                    CalendarItemCreateOrDeleteOperationType sendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToNone;
                    createItemRequest.SendMeetingInvitations = (CalendarItemCreateOrDeleteOperationType)sendMeetingInvitations;
                    createItemRequest.SendMeetingInvitationsSpecified = true;
                    CalendarItemType calendar = new CalendarItemType();
                    calendar.Subject = itemSubject;
                    item = calendar;
                    break;
                case DistinguishedFolderIdNameType.inbox:
                    MessageType message = new MessageType();
                    message.Subject = itemSubject;
                    item = message;
                    break;
                case DistinguishedFolderIdNameType.tasks:
                    TaskType taskItem = new TaskType();
                    taskItem.Subject = itemSubject;
                    item = taskItem;
                    break;
                default:
                    Site.Assert.Fail("The parent folder type '{0}' is invalid and the valid folder types are: contacts, calendar, inbox and tasks.", parentFolderType.ToString());
                    break;
            }

            // Set the MessageDisposition property.
            MessageDispositionType messageDisposition = MessageDispositionType.SaveOnly;
            createItemRequest.MessageDisposition = (MessageDispositionType)messageDisposition;
            createItemRequest.MessageDispositionSpecified = true;

            // Specify the folder in which new items are saved.
            createItemRequest.SavedItemFolderId = new TargetFolderIdType();
            FolderIdType folderId = new FolderIdType();
            folderId.Id = parentFolderId;
            createItemRequest.SavedItemFolderId.Item = folderId;

            // Specify the collection of items to be created.
            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items = new ItemType[] { item };

            // Initialize the ID of the created item.
            ItemIdType createdItemId = null;

            // Invoke the create item operation and get the response.
            CreateItemResponseType createItemResponse = this.COREAdapter.CreateItem(createItemRequest);

            if (createItemResponse != null && createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
            {
                ItemInfoResponseMessageType info = createItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
                Site.Assert.IsNotNull(info, "The items in CreateItem response should not be null.");

                // Get the ID of the created item.
                createdItemId = info.Items.Items[0].ItemId;
            }

            return createdItemId;
        }
        public void MSOXWSFOLD_S01_TC10_CreateMultipleManagedFolder()
        {
            #region Create two new managed folders

            CreateManagedFolderRequestType createManagedFolderRequest = this.GetCreateManagedFolderRequest(new string[] { Common.GetConfigurationPropertyValue("ManagedFolderName1", this.Site), Common.GetConfigurationPropertyValue("ManagedFolderName2", this.Site) });

            // Create the specified managed folder.
            CreateManagedFolderResponseType createManagedFolderResponse = this.FOLDAdapter.CreateManagedFolder(createManagedFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(createManagedFolderResponse, 2, this.Site);

            FolderIdType newFolderId1 = ((FolderInfoResponseMessageType)createManagedFolderResponse.ResponseMessages.Items[0]).Folders[0].FolderId;
            FolderIdType newFolderId2 = ((FolderInfoResponseMessageType)createManagedFolderResponse.ResponseMessages.Items[1]).Folders[0].FolderId;

            // Save the new created managed folder's folder id.
            this.NewCreatedFolderIds.Add(newFolderId1);
            this.NewCreatedFolderIds.Add(newFolderId2);

            #endregion

            #region Get the new created managed folder

            // Create a FolderId array to save newFolderId1 and 2.
            FolderIdType[] folderIds = new FolderIdType[2] { newFolderId1, newFolderId2 };

            // GetFolder request.
            GetFolderType getFolderRequest = this.GetGetFolderRequest(DefaultShapeNamesType.AllProperties, folderIds);

            // Get the specified managed folder.
            GetFolderResponseType getFolderResponse = this.FOLDAdapter.GetFolder(getFolderRequest);

            // Check the response.
            Common.CheckOperationSuccess(getFolderResponse, 2, this.Site);

            #endregion

            // Managed folder ids.
            string firstManagedFolderId = ((FolderInfoResponseMessageType)getFolderResponse.ResponseMessages.Items[0]).Folders[0].ManagedFolderInformation.ManagedFolderId;
            string secondManagedFolderId = ((FolderInfoResponseMessageType)getFolderResponse.ResponseMessages.Items[1]).Folders[0].ManagedFolderInformation.ManagedFolderId;

            // Add the debug information
            this.Site.Log.Add(LogEntryKind.Debug, "Verify MS-OXWSFOLD_R1091 \nThe ManageFolderId of the first managed folder is: {0}, and the ManageFolderId of the second managed folder is: {1}. If they are equal to each other, this requirement will be captured", firstManagedFolderId, secondManagedFolderId);

            // Verify MS-OXWSFOLD requirement: MS-OXWSFOLD_R1091
            bool isVerifiedR1091 = firstManagedFolderId != secondManagedFolderId;

            this.Site.CaptureRequirementIfIsTrue(
                isVerifiedR1091,
                1091,
                @"[In t:ManagedFolderInformationType Complex Type]ManageFolderId is the unique identifier of a managed folder.");
        }
        /// <summary>
        /// Deletes all items and sub folders from the specified folder.
        /// </summary>
        /// <param name="folderName">Name of the specified parent folder.</param>
        /// <param name="needVerify">If need to verify the subfolders,when false,it will cleanup all the folders without verify</param>
        /// <returns>If the specified folder is cleaned up successfully, return true; otherwise, return false.</returns>
        private bool CleanupFolder(DistinguishedFolderIdNameType folderName, bool needVerify = true)
        {
            bool isAllItemsAndFoldersDeleted = false;
            ItemType[] items = this.FindAllItems(folderName);

            // Create a request for the DeleteItem operation.
            DeleteItemType deleteItemRequest = new DeleteItemType();

            // The item is permanently removed from the store.
            deleteItemRequest.DeleteType = DisposalType.HardDelete;

            // Do not send meeting cancellations.
            deleteItemRequest.SendMeetingCancellations = CalendarItemCreateOrDeleteOperationType.SendToNone;
            deleteItemRequest.SendMeetingCancellationsSpecified = true;

            if (items != null)
            {
                foreach (ItemType currentItem in items)
                {
                    if (currentItem.GetType() == typeof(TaskType))
                    {
                        deleteItemRequest.AffectedTaskOccurrencesSpecified = true;
                        deleteItemRequest.AffectedTaskOccurrences = AffectedTaskOccurrencesType.AllOccurrences;
                    }

                    deleteItemRequest.ItemIds = new BaseItemIdType[] { currentItem.ItemId };

                    // Invoke the delete item operation.
                    DeleteItemResponseType response = this.exchangeServiceBinding.DeleteItem(deleteItemRequest);

                    Site.Assert.AreEqual<ResponseClassType>(
                            ResponseClassType.Success,
                            response.ResponseMessages.Items[0].ResponseClass,
                            "The delete item operation should execute successfully.");
                }
            }

            // Find all sub folders in the specified folder.
            BaseFolderType[] folders = this.FindAllSubFolders(folderName);

            if (folders.Length != 0)
            {
                foreach (BaseFolderType currentFolder in folders)
                {
                    if (needVerify)
                    {
                        bool isCreatedByCase = false;
                        AdapterHelper.CreatedFolders.ForEach(r =>
                        {
                            if (r.FolderId.Id == currentFolder.FolderId.Id)
                            {
                                isCreatedByCase = true;
                            }
                        });

                        if (!isCreatedByCase)
                        {
                            continue;
                        }
                    }

                    FolderIdType responseFolderId = currentFolder.FolderId;

                    FolderIdType folderId = new FolderIdType();
                    folderId.Id = responseFolderId.Id;

                    DeleteFolderType deleteFolderRequest = new DeleteFolderType();
                    deleteFolderRequest.DeleteType = DisposalType.HardDelete;
                    deleteFolderRequest.FolderIds = new BaseFolderIdType[1];
                    deleteFolderRequest.FolderIds[0] = folderId;

                    // Send the request and get the response.
                    DeleteFolderResponseType deleteFolderResponse = this.exchangeServiceBinding.DeleteFolder(deleteFolderRequest);

                    // Delete folder operation should return response info.
                    if (deleteFolderResponse.ResponseMessages.Items[0] != null)
                    {
                        Site.Assert.AreEqual<ResponseClassType>(
                            ResponseClassType.Success,
                            deleteFolderResponse.ResponseMessages.Items[0].ResponseClass,
                            "The delete folder operation should be successful.");
                    }
                }
            }

            // Invoke the FindItem operation.
            items = this.FindAllItems(folderName);

            // Invoke the FindFolder operation.
            folders = this.FindAllSubFolders(folderName);

            // If neither items and sub folders could be found, the folder has been cleaned up successfully.
            bool allFoldersBelongstoSys = true;

            foreach (BaseFolderType folder in folders)
            {               
                bool find = false;
                AdapterHelper.CreatedFolders.ForEach(r =>
                {
                    if (r.FolderId.Id == folder.FolderId.Id)
                    {
                        find = true;
                    }
                });

                if (find)
                {
                    allFoldersBelongstoSys = false;
                    break;
                }
            }

            if (items == null && (folders.Length == 0 || allFoldersBelongstoSys))
            {
                isAllItemsAndFoldersDeleted = true;
            }

            return isAllItemsAndFoldersDeleted;
        }
        /// <summary>
        /// Find the specified sub folder.
        /// </summary>
        /// <param name="parentFolderName">Name of the specified parent folder.</param>
        /// <param name="subFolderName">Name of the specified sub folder.</param>
        /// <returns>Id of the folder.</returns>
        private FolderIdType FindSubFolder(DistinguishedFolderIdNameType parentFolderName, string subFolderName)
        {
            FolderIdType folderId = new FolderIdType();

            // Find all sub folders in the specified parent folder.
            BaseFolderType[] folders = this.FindAllSubFolders(parentFolderName);
            Site.Assert.IsNotNull(folders, "There should be at least one folder in the '{0}' folder.", parentFolderName);

            // Find the item with the specified subject.
            foreach (BaseFolderType currentFolder in folders)
            {
                if (currentFolder.DisplayName == subFolderName)
                {
                    folderId = currentFolder.FolderId;
                    break;
                }
            }

            Site.Assert.IsNotNull(folderId.Id, "There should be at least one folder with the specified subject '{0}'.", subFolderName);
            return folderId;
        }
        /// <summary>
        /// Log on to a mailbox with a specified user account and delete all the subfolders from the specified 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="folderName">Name of the folder which should be cleaned up.</param>
        /// <param name="destFolderName">The name of the destination folder which will be deleted.</param>
        /// <returns>If the folder is cleaned up successfully, return true; otherwise, return false.</returns>
        public bool CleanupFolder(string userName, string userPassword, string userDomain, string folderName, string destFolderName)
        {
            // Log on mailbox with specified user mailbox.
            this.exchangeServiceBinding.Credentials = new NetworkCredential(userName, userPassword, userDomain);
            #region Delete all sub folders and the items in these sub folders in the specified parent folder.
            // Parse the parent folder name.
            DistinguishedFolderIdNameType parentFolderName = (DistinguishedFolderIdNameType)Enum.Parse(typeof(DistinguishedFolderIdNameType), folderName, true);

            // Create an array of BaseFolderType.
            BaseFolderType[] folders = null;

            // 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 = parentFolderName;

            // Add the folders to search to the request.
            findFolderRequest.ParentFolderIds = folderIDArray;

            // Invoke FindFolder operation and get the response.
            FindFolderResponseType findFolderResponse = this.exchangeServiceBinding.FindFolder(findFolderRequest);

            // If there are folders found under the specified folder, delete all of them.
            if (findFolderResponse != null && findFolderResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
            {
                // Get the folders from the response.
                FindFolderResponseMessageType findFolderResponseMessageType = findFolderResponse.ResponseMessages.Items[0] as FindFolderResponseMessageType;
                Site.Assert.IsNotNull(findFolderResponseMessageType, "The items in FindFolder response should not be null.");

                folders = findFolderResponseMessageType.RootFolder.Folders;
                if (folders.Length != 0)
                {
                    ////Indicates whether the destination folder was found and removed.
                    bool found = false;

                    // Loop to delete all the found folders.
                    foreach (BaseFolderType currentFolder in folders)
                    {
                        if (string.Compare(currentFolder.DisplayName, destFolderName, StringComparison.InvariantCultureIgnoreCase) != 0)
                        {
                            continue;
                        }

                        FolderIdType responseFolderId = currentFolder.FolderId;

                        FolderIdType folderId = new FolderIdType();
                        folderId.Id = responseFolderId.Id;

                        DeleteFolderType deleteFolderRequest = new DeleteFolderType();
                        deleteFolderRequest.DeleteType = DisposalType.HardDelete;
                        deleteFolderRequest.FolderIds = new BaseFolderIdType[1];
                        deleteFolderRequest.FolderIds[0] = folderId;

                        // Invoke DeleteFolder operation and get the response.
                        DeleteFolderResponseType deleteFolderResponse = this.exchangeServiceBinding.DeleteFolder(deleteFolderRequest);

                        Site.Assert.AreEqual<ResponseClassType>(
                            ResponseClassType.Success,
                            deleteFolderResponse.ResponseMessages.Items[0].ResponseClass,
                            "The delete folder operation should be successful.");

                        found = true;
                        break;
                    }

                    Site.Assert.IsTrue(
                           found,
                           "The destination folder can not be found in the assigned parent folder.");
                }
            }
            #endregion

            #region Check whether sub folders are deleted successfully.
            // Invoke the FindFolder operation again.
            findFolderResponse = this.exchangeServiceBinding.FindFolder(findFolderRequest);

            Site.Assert.AreEqual<ResponseCodeType>(
                ResponseCodeType.NoError,
                findFolderResponse.ResponseMessages.Items[0].ResponseCode,
                string.Format(
                "The delete folder operation should be successful. Expected response code: {0}, actual response code: {1}",
                ResponseCodeType.NoError,
                findFolderResponse.ResponseMessages.Items[0].ResponseCode));

            // Get the found folders from the response.
            FindFolderResponseMessageType findFolderResponseMessage = findFolderResponse.ResponseMessages.Items[0] as FindFolderResponseMessageType;
            folders = findFolderResponseMessage.RootFolder.Folders;

            // If no sub folders that created by case could be found, the folder has been cleaned up successfully.
            foreach (BaseFolderType folder in folders)
            {
                if (string.Compare(folder.DisplayName, destFolderName, StringComparison.InvariantCultureIgnoreCase) != 0)
                {
                    continue;
                }
                else
                {
                    return false;
                }
            }

            return true;
            #endregion
        }