Odata $filter for the date in the Office 365 REST API

会有一股神秘感。 提交于 2019-12-11 03:43:30

问题


I'm trying to retrieve events for a user from its Office 365 account using the REST API but filtered by the field "Start". For instance, retrieve all the events that will happen after the date: 2014-08-29T09:13:28' but i'm always retrieving this error message

{
  "error": {
    "code": "ErrorInvalidUrlQuery",
    "message": "The query parameter '$filter' is invalid.",
    "innererror": {
      "message": "The query parameter '$filter' is invalid.",
      "type": "Microsoft.Exchange.Services.OData.InvalidUrlQueryException",
      "stacktrace": " at Microsoft.Exchange.Services.OData.Web.ODataQueryOptions.Populate()
 at Microsoft.Exchange.Services.OData.ODataContext..ctor(HttpContext httpContext, Uri requestUri, ServiceModel serviceModel, ODataPathWrapper odataPath, ODataUriParser odataUriParser)
 at Microsoft.Exchange.Services.OData.Web.RequestBroker.InitializeODataContext()
 at Microsoft.Exchange.Services.OData.Web.RequestBroker.Process()",
      "internalexception": {
        "message": "A binary operator with incompatible types was detected. Found operand types 'Edm.DateTimeOffset' and 'Edm.Int32' for operator kind 'Equal'.",
        "type": "Microsoft.OData.Core.ODataException",
        "stacktrace": " at Microsoft.OData.Core.UriParser.Parsers.BinaryOperatorBinder.PromoteOperandTypes(BinaryOperatorKind binaryOperatorKind, SingleValueNode& left, SingleValueNode& right)
 at Microsoft.OData.Core.UriParser.Parsers.BinaryOperatorBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)
 at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token)
 at Microsoft.OData.Core.UriParser.Parsers.FilterBinder.BindFilter(QueryToken filter)
 at Microsoft.OData.Core.UriParser.ODataUriParser.ParseFilterImplementation(String filter, IEdmType elementType, IEdmNavigationSource navigationSource)
 at Microsoft.OData.Core.UriParser.ODataUriParser.ParseFilter()
 at Microsoft.Exchange.Services.OData.Web.ODataQueryOptions.Populate()"
      }
    }
  }
}

Here all the thing i've tried:

[ressource]=https://outlook.office365.com/

url: [ressource]EWS/OData/Me/Events?$filter=Start%20eq%20DateTime'2012-05-29T09:13:28'

url: [ressource]EWS/OData/Me/Events?$filter=Start%20eq%20DateTime'20141231'

url: [ressource]EWS/OData/Me/Events?$filter=month(Start)%20eq%2012

And i've tried the $filter on string field or the $select on the field Start and it worked perfectly so i think my error is caused by the date format but i don't know how to solve it, so if anybody could help me on that, it woud be great!

Thank you!


回答1:


Filtering on dates does work, but you can't do everything in the OData v4 spec. For example, these all work for me:

/Me/Events?$filter=Start eq 2014-08-28T21:00:00Z
/Me/Events?$filter=Start ge 2014-08-28T21:00:00Z

Adding the "DateTime" in front of it makes it fail. The same with the month function. If you stick with the format above it should work for you.




回答2:


In V2 of the O365 REST API you should use:

URL encoded:     $filter=Start/DateTime%20ge%20%272017-03-15T00:00:00Z%27
Not URL encoded: $filter=Start/DateTime ge '2017-03-15T00:00:00Z'

More info https://msdn.microsoft.com/en-us/office/office365/api/complex-types-for-mail-contacts-calendar#filter-requests




回答3:


Hi Just increase top=100 or more whatever you want to pull from your inbox.

https://outlook.office365.com/api/v2.0/me/MailFolders/Inbox/messages/?%24select=CreatedDateTime%2cLastModifiedDateTime%2cChangeKey%2cCategories%2cReceivedDateTime%2cSentDateTime%2cHasAttachments%2cSubject%2cBody%2cImportance%2cParentFolderId%2cSender%2cFrom%2cToRecipients%2cCcRecipients%2cBccRecipients%2cReplyTo%2cConversationId%2cIsDeliveryReceiptRequested%2cIsReadReceiptRequested%2cIsRead%2cIsDraft%2cWebLink&%24top=100&%24skip=0"


来源:https://stackoverflow.com/questions/25574361/odata-filter-for-the-date-in-the-office-365-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!