/// <summary>
 /// Static constructor for initializing our property path
 /// </summary>
 static TaskType()
 {
     CorrectedDelegationStatePath = new PathToExtendedFieldType();
     CorrectedDelegationStatePath.PropertySetId =
           "{00062003-0000-0000-C000-000000000046}";
     CorrectedDelegationStatePath.PropertyId = 0x8113;
     CorrectedDelegationStatePath.PropertyType =
           MapiPropertyTypeType.Integer;
 }
 /// <summary>
 /// Creates an IsEqualTo clause for our search key (Listing 5-14 cont'd)
 /// </summary>
 /// <param name="searchKeyPath">Search key path</param>
 /// <param name="value">base64 search key to look for</param>
 /// <returns>IsEqualTo clause</returns>
 /// 
 public static IsEqualToType CreateIsEqualToSearchKey(
                         PathToExtendedFieldType searchKeyPath,
                         string value)
 {
     IsEqualToType isEqualTo = new IsEqualToType();
     isEqualTo.Item = searchKeyPath;
     isEqualTo.FieldURIOrConstant = new FieldURIOrConstantType();
     ConstantValueType constant = new ConstantValueType();
     constant.Value = value;
     isEqualTo.FieldURIOrConstant.Item = constant;
     return isEqualTo;
 }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="fieldURI">FieldURI representing metadata about the property</param>
        /// <param name="values">PARAMS array of values for multivalued property</param>
        /// 
        public ExtendedPropertyType(PathToExtendedFieldType fieldURI, params string[] values)
        {
            this.ExtendedFieldURI = fieldURI;
            NonEmptyArrayOfPropertyValuesType array = new NonEmptyArrayOfPropertyValuesType();
            array.Items = new string[values.Length];

            int index = 0;
            foreach (string value in values)
            {
                array.Items[index++] = value;
            }

            this.Item = array;
        }
 /// <summary>
 /// Creates a GuidId extended field uri 
 /// </summary>
 /// <param name="guid">Guid representing the property set</param>
 /// <param name="propId">Property id of the named property</param>
 /// <param name="propType">Property type</param>
 /// <returns>PathToExtendedFieldType proxy object</returns>
 ///
 public static PathToExtendedFieldType BuildGuidId(
                                             Guid guid,
                                             int propId,
                                             MapiPropertyTypeType propType)
 {
     PathToExtendedFieldType result = new PathToExtendedFieldType();
     result.PropertyId = propId;
     // Don’t forget to set the specified property to true for optional value
     // types!!
     //
     result.PropertyIdSpecified = true;
     result.PropertySetId = guid.ToString("D");
     result.PropertyType = propType;
     return result;
 }
        /// <summary>
        /// Copy items to a destination folder and return the new ids for these items (Listing 5-14)
        /// </summary>
        /// <param name="binding">Exchange binding to use for the call</param>
        /// <param name="destinationFolderId">Destination for the items</param>
        /// <param name="itemsToCopy">Items to copy</param>
        /// <returns>List of new item ids</returns>
        /// 
        public List<ItemIdType> CopyItemEx(
                             BaseFolderIdType destinationFolderId,
                             List<BaseItemIdType> itemsToCopy)
        {
            // STEP 1:  First, we need to retrieve some unique information about
            // each item.  Let's use the PR_SEARCH_KEY. Note that extended properties are
            // discussed in Chapter 13, "Extended Properties"
            //
            GetItemType getSearchKeyRequest = new GetItemType();

            PathToExtendedFieldType searchKeyPath = new PathToExtendedFieldType();
            searchKeyPath.PropertyTag = "0x300B";
            searchKeyPath.PropertyType = MapiPropertyTypeType.Binary;

            // Use ItemResponseShapeType overload from chapter 3. We want the Id and the
            // search key
            //
            ItemResponseShapeType idAndSearchKeyShape = new ItemResponseShapeType(
                                     DefaultShapeNamesType.IdOnly,
                                     searchKeyPath);
            getSearchKeyRequest.ItemShape = idAndSearchKeyShape;
            getSearchKeyRequest.ItemIds = itemsToCopy.ToArray();

            // Get the items
            //
            GetItemResponseType getSearchKeyResponse =
                       this.GetItem(getSearchKeyRequest);
            List<string> base64SearchKeys = new List<string>(
                         getSearchKeyResponse.ResponseMessages.Items.Length);

            // For each item, add the search keys to our list
            //
            foreach (ItemInfoResponseMessageType searchKeyMessage in
                          getSearchKeyResponse.ResponseMessages.Items)
            {
                ExtendedPropertyType searchKeyProperty =
                        searchKeyMessage.Items.Items[0].ExtendedProperty[0];
                base64SearchKeys.Add((string)searchKeyProperty.Item);
            }

            // Now we have a list of the search keys for the items that we want to
            // copy.
            // STEP 2:  Perform the copy

            CopyItemType copyItemRequest = new CopyItemType();
            copyItemRequest.ToFolderId = new TargetFolderIdType();
            copyItemRequest.ToFolderId.Item = destinationFolderId;

            // just copy the array from our GetItem request rather than building a
            // new one.
            //
            copyItemRequest.ItemIds = getSearchKeyRequest.ItemIds;
            CopyItemResponseType copyResponse = this.CopyItem(copyItemRequest);

            // Now, we know that we do not get new ids from the above request, but
            // we (read: you) SHOULD check the response code for each of the copies
            // operations.
            //
            // STEP 3:  For each successful copy, we want to find the items by
            // search key.
            //
            FindItemType findBySearchKey = new FindItemType();
            findBySearchKey.ItemShape = idAndSearchKeyShape;
            findBySearchKey.ParentFolderIds = new BaseFolderIdType[] {
              destinationFolderId };
            findBySearchKey.Traversal = ItemQueryTraversalType.Shallow;
            findBySearchKey.Restriction = new RestrictionType();

            // Here we need to build up our query.  Rather than issuing several
            // FindItem calls, let's build up a single OR restriction here with a
            // bunch of items. Note that EWS restricts filter depths, so we
            // might need to break this up depending on how many items we are
            // copying...
            //
            if (base64SearchKeys.Count > 1)
            {
                OrType or = new OrType();
                List<IsEqualToType> orChildren = new List<IsEqualToType>();
                foreach (string searchKey in base64SearchKeys)
                {
                    // Note that CreateIsEqualToSearchKey is implemented on the partial class
                    // extension of RestrictionType.
                    //
                    IsEqualToType isEqualTo = RestrictionType.CreateIsEqualToSearchKey(
                          searchKeyPath, searchKey);
                    orChildren.Add(isEqualTo);
                }
                or.Items = orChildren.ToArray();

                findBySearchKey.Restriction.Item = or;
            }
            else
            {
                // we only have one item.  No need for the OR clause
                //
                IsEqualToType isEqualTo = RestrictionType.CreateIsEqualToSearchKey(
                                searchKeyPath, base64SearchKeys[0]);
                findBySearchKey.Restriction.Item = isEqualTo;
            }

            FindItemResponseType findResponse = this.FindItem(findBySearchKey);

            // Since we searched in a single target folder, we will have a single
            // response message
            //
            FindItemResponseMessageType findResponseMessage =
                findResponse.ResponseMessages.Items[0] as FindItemResponseMessageType;
            ItemType[] foundItems = (findResponseMessage.RootFolder.Item as
                                        ArrayOfRealItemsType).Items;
            List<ItemIdType> newIds = new List<ItemIdType>();
            foreach (ItemType item in foundItems)
            {
                newIds.Add(item.ItemId);
            }
            return newIds;
        }
 /// <summary>
 /// Creates a GuidId extended field URI for a distinguished property set id
 /// </summary>
 /// <param name="propertySet">DistinguishedPropertySetId</param>
 /// <param name="propId">dispatch Id</param>
 /// <param name="propType">Property type</param>
 /// <returns>PathToExtendedFieldType</returns>
 /// 
 public static PathToExtendedFieldType BuildGuidId(
                   DistinguishedPropertySetType propertySet,
                   int propId,
                   MapiPropertyTypeType propType)
 {
     PathToExtendedFieldType result = new PathToExtendedFieldType();
     result.PropertyId = propId;
     result.PropertyIdSpecified = true;
     result.DistinguishedPropertySetId = propertySet;
     result.DistinguishedPropertySetIdSpecified = true;
     result.PropertyType = propType;
     return result;
 }
 /// <summary>
 /// Creates a prop tag extended field uri (proxy)
 /// </summary>
 /// <param name="propId">16-bit Id of property tag</param>
 /// <param name="propType">property type</param>
 /// <returns>PathToExtendedFieldType proxy object</returns>
 ///
 public static PathToExtendedFieldType BuildPropertyTag(
                                         ushort propId,
                                         MapiPropertyTypeType propType)
 {
     PathToExtendedFieldType result = new PathToExtendedFieldType();
     result.PropertyTag = string.Format("0x{0:x}", propId);
     result.PropertyType = propType;
     return result;
 }
 /// <summary>
 /// Build a guid/name extended property path with DisinguishedPropertySetId
 /// </summary>
 /// <param name="propertySetId">DistinguishedPropertySetId</param>
 /// <param name="propertyName">Property Name</param>
 /// <param name="propertyType">Property Type</param>
 /// <returns>PathToExtendedFieldType</returns>
 /// 
 public static PathToExtendedFieldType BuildGuidName(
                      DistinguishedPropertySetType propertySetId,
                      string propertyName,
                      MapiPropertyTypeType propertyType)
 {
     PathToExtendedFieldType result = new PathToExtendedFieldType();
     result.DistinguishedPropertySetId = propertySetId;
     result.DistinguishedPropertySetIdSpecified = true;
     result.PropertyName = propertyName;
     result.PropertyType = propertyType;
     return result;
 }
 /// <summary>
 /// Builds a guid/name extended property
 /// </summary>
 /// <param name="guid">Property set guid</param>
 /// <param name="propertyName">Property name</param>
 /// <param name="propType">Property type</param>
 /// <returns>Guid/Name extended property</returns>
 /// 
 public static PathToExtendedFieldType BuildGuidName(
                                         Guid guid,
                                         string propertyName,
                                         MapiPropertyTypeType propType)
 {
     PathToExtendedFieldType result = new PathToExtendedFieldType();
     result.PropertySetId = guid.ToString("D");
     result.PropertyName = propertyName;
     result.PropertyType = propType;
     return result;
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="fieldURI">FieldURI representing metadata about the property</param>
 /// <param name="value">Value for the property</param>
 /// 
 public ExtendedPropertyType(PathToExtendedFieldType fieldURI, string value)
 {
     this.ExtendedFieldURI = fieldURI;
     this.Item = value;
 }