开发者

EWS - How to search for items [message] between dates?

开发者 https://www.devze.com 2022-12-25 16:19 出处:网络
I am trying to search for message items between two dates from the inbox folder. I use the following restrictionType but it throws this error:

I am trying to search for message items between two dates from the inbox folder. I use the following restrictionType but it throws this error:

firmt.RootFolder = null

What am I doing wrong?

There is some messages between the mentionned dates ;-)

Thanks for your suggestions.

        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 
            findItemRe开发者_如何学Pythonquest.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());
            }


I found the answer on my own after a long search about date format.

The restrictions has to be defined as this:

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

Hope this help someone some day ;-)


In case anyone stumbles upon this in the future, EWS has gotten even more strict about date formatting. The accepted answer formatting works for 2 digit months, but it does not for single digit months.

The formatting that works in all cases is:

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


The restriction also works using the "Sortable date/time pattern".

Datetime.Now.ToString("s")
0

精彩评论

暂无评论...
验证码 换一张
取 消