2010-03-30 56 views
4

我正在嘗試從收件箱文件夾中搜索兩個日期之間的郵件項目。 我用下面的restrictionType但它拋出這個錯誤:EWS - 如何在日期間搜索項目[消息]?

firmt.RootFolder = NULL

我在做什麼錯?

提到的日期之間有一些消息;-)

感謝您的建議。

 using (ExchangeServiceBinding esb = new ExchangeServiceBinding()) 
     { 
      esb.Url = ConfigurationManager.AppSettings["ExchangeWebServicesURL"].ToString(); 
      esb.RequestServerVersionValue = new RequestServerVersion(); 
      esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1; 
      esb.PreAuthenticate = true; 
      esb.Credentials = new NetworkCredential(email, password); 
      FindItemType findItemRequest = new FindItemType(); 
      // paging 
      IndexedPageViewType ipvt = new IndexedPageViewType(); 
      ipvt.BasePoint = IndexBasePointType.Beginning; 
      ipvt.MaxEntriesReturned = nombreMessage; 
      ipvt.MaxEntriesReturnedSpecified = true; 
      ipvt.Offset = offset; 
      findItemRequest.Item = ipvt; 
      // filter by dates 
      AndType andType = new AndType(); 
      List<SearchExpressionType> searchExps = new List<SearchExpressionType>(); 
      RestrictionType restriction = new RestrictionType(); 
      PathToUnindexedFieldType pteft = new PathToUnindexedFieldType 
      { 
       FieldURI = UnindexedFieldURIType.itemDateTimeSent 
      }; 
      IsGreaterThanOrEqualToType IsGreaterThanOrEqualTo = new IsGreaterThanOrEqualToType 
      { 
       Item = pteft, 
       FieldURIOrConstant = new FieldURIOrConstantType 
       { 
        Item = new ConstantValueType 
        { 
         Value = DateTime.Today.AddDays(-6d).ToString() 
        } 
       } 
      }; 
      searchExps.Add(IsGreaterThanOrEqualTo); 

      IsLessThanOrEqualToType IsLessThanOrEqualTo = new IsLessThanOrEqualToType 
      { 
       Item = pteft, 
       FieldURIOrConstant = new FieldURIOrConstantType 
       { 
        Item = new ConstantValueType 
        { 
         Value = DateTime.Today.AddDays(1d).ToString() 
        } 
       } 
      }; 
      searchExps.Add(IsLessThanOrEqualTo); 
      andType.Items = searchExps.ToArray(); 
      restriction.Item = andType; 
      findItemRequest.Restriction = restriction; 
      //// Define the sort order of items. 
      FieldOrderType[] fieldsOrder = new FieldOrderType[1]; 
      fieldsOrder[0] = new FieldOrderType(); 
      PathToUnindexedFieldType dateOrder = new PathToUnindexedFieldType 
      { 
       FieldURI = UnindexedFieldURIType.itemDateTimeReceived 
      }; 
      fieldsOrder[0].Item = dateOrder; 
      fieldsOrder[0].Order = SortDirectionType.Descending; 
      findItemRequest.SortOrder = fieldsOrder; 
      findItemRequest.Traversal = ItemQueryTraversalType.Shallow; 
      // define which item properties are returned in the response 
      findItemRequest.ItemShape = new ItemResponseShapeType 
      { 
       BaseShape = DefaultShapeNamesType.IdOnly 
      }; 
      // identify which folder to search 
      DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1]; 
      folderIDArray[0] = new DistinguishedFolderIdType { Id = DistinguishedFolderIdNameType.inbox }; 
      // add folders to request 
      findItemRequest.ParentFolderIds = folderIDArray; 
      // find the messages 
      FindItemResponseType findItemResponse = esb.FindItem(findItemRequest); 
      //------------- 
      ArrayOfResponseMessagesType responseMessages = findItemResponse.ResponseMessages; 
      ResponseMessageType responseMessage = responseMessages.Items[0]; 
      if (responseMessage is FindItemResponseMessageType) 
      { 
       FindItemResponseMessageType firmt = (responseMessage as FindItemResponseMessageType); 
     *******FindItemParentType fipt = firmt.RootFolder;******** 
       object obj = fipt.Item; 
       // FindItem contains an array of items. 
       ArrayOfRealItemsType realitems = (obj as ArrayOfRealItemsType); 
       ItemType[] items = realitems.Items; 
       // if no messages were found, then return null -- we're done 
       if (items == null || items.Count() <= 0) 
        return null; 
       // FindItem never gets "all" the properties, so now that we've found them all, we need to get them all. 
       BaseItemIdType[] itemIds = new BaseItemIdType[items.Count()]; 
       for (int i = 0; i < items.Count(); i++) 
        itemIds[i] = items[i].ItemId; 
       GetItemType getItemType = new GetItemType 
       { 
        ItemIds = itemIds, 
        ItemShape = new ItemResponseShapeType 
        { 
         BaseShape = DefaultShapeNamesType.AllProperties, 
         BodyType = BodyTypeResponseType.Text, 
         BodyTypeSpecified = true, 
         AdditionalProperties = new BasePathToElementType[] { 
          new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.itemDateTimeSent }, 
          new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageFrom }, 
          new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageIsRead }, 
          new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageSender }, 
          new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageToRecipients }, 
          new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageCcRecipients }, 
          new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageBccRecipients } 
         } 
        } 
       }; 
       GetItemResponseType getItemResponse = esb.GetItem(getItemType); 
       messages = ReadItems(getItemResponse, items.Count()); 
      } 
+0

在什麼行是拋出的異常?這是非常多的代碼。您需要能夠確定問題發生的位置,而不是發佈整個程序。 – 2010-03-30 11:54:53

+0

是的,對不起,但因爲我不知道問題出在哪裏,所以我發佈了大量的代碼。 例外情況位於星號爲**********的行後面,並且fipt的值爲空。 深入調試會話後,我發現responseMessage.MessageText具有以下內容「指定的值對於屬性無效」。並且指定的值是「FieldURI」。這裏是MessageXml屬性值:值爲空。 – SomFred 2010-03-30 12:32:36

回答

4

經過長時間搜索日期格式後,我自己找到了答案。

的限制已被定義爲這樣:

// greater or equal to 
string dateStart = DateTime.Today.add(-6d); 
string dateEnd = DateTime.Today.Add(1d); 

PathToUnindexedFieldType dateSentPath = new PathToUnindexedFieldType(); 
dateSentPath.FieldURI = UnindexedFieldURIType.itemDateTimeSent; 
IsGreaterThanOrEqualToType IsGreaterThanOrEqual = new IsGreaterThanOrEqualToType(); 
IsGreaterThanOrEqual.Item = dateSentPath; 
FieldURIOrConstantType dateConstant = new FieldURIOrConstantType(); 
ConstantValueType dateConstantValue = new ConstantValueType(); 
dateConstantValue.Value = string.Format("{0}-{1}-{2}T00:00:00Z", dateStart.Year.ToString(), dateStart.Month.ToString(), dateStart.Day.ToString()); 
dateConstant.Item = dateConstantValue; 
IsGreaterThanOrEqual.FieldURIOrConstant = dateConstant; 

// less than or equal to 
PathToUnindexedFieldType dateSentPath1 = new PathToUnindexedFieldType(); 
dateSentPath1.FieldURI = UnindexedFieldURIType.itemDateTimeSent; 
IsLessThanOrEqualToType lessThanOrEqualTo = new IsLessThanOrEqualToType(); 
lessThanOrEqualTo.Item = dateSentPath1; 
FieldURIOrConstantType dateConstant1 = new FieldURIOrConstantType(); 
ConstantValueType dateConstantValue1 = new ConstantValueType(); 
dateConstantValue1.Value = string.Format("{0}-{1}-{2}T00:00:00Z", dateEnd.Year.ToString(), dateEnd.Month.ToString(), dateEnd.Day.ToString()); 
dateConstant1.Item = dateConstantValue1; 
lessThanOrEqualTo.FieldURIOrConstant = dateConstant1; 

RestrictionType restriction = new RestrictionType(); 
AndType andType = new AndType(); 
andType.Items = new SearchExpressionType[] { lessThanOrEqualTo, IsGreaterThanOrEqual }; 
restriction.Item = andType; 
findItemRequest.Restriction = restriction; 

希望這有助於有人有一天;-)

0

在任何情況下,在未來絆倒在此,EWS已經變得甚至更加嚴格日期格式。接受的答案格式適用於2位數月份,但不適用於單位數月份。

,在所有情況都適用的格式是:

DateTime.Today.AddDays(15).ToString("yyyy-MM-ddThh:mm:ssZ") 
0

的限制也適用使用「可排序日期/時間模式」。

Datetime.Now.ToString("s")