Esempio n. 1
0
        /// <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>
        /// 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
        }