5

I am trying to filter standard Calendar events via SharePoint web api and my query looks like this:

/_api/web/lists/getbytitle('calendar')/items?$filter=( EventDate  ge  datetime'2013-01-26T22:00:00Z')&$select=Title,EventDate,ID 

Query doesn't work and I receive this error:

The field 'EventDate' of type 'DateTime' cannot be used in the query filter expression

Error Details:

{"error":{"code":"-1, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The field 'EventDate' of type 'DateTime' cannot be used in the query filter expression."},"innererror":{"message":"The field 'EventDate' of type 'DateTime' cannot be used in the query filter expression.","type":"Microsoft.SharePoint.SPException","stacktrace":"   at Microsoft.SharePoint.SPListItemEntityCollectionCamlQueryBuilder.CheckFieldRefUsage(SPField field, FieldRefUsage fieldRefUsage)\r\n   at Microsoft.SharePoint.SPListItemEntityCollectionCamlQueryBuilder.SetFieldRef(XmlWriter writer, String fieldPath, FieldRefUsage fieldRefUsage, Action`1 attributeAction)\r\n   at Microsoft.SharePoint.SPListItemEntityCollectionCamlQueryBuilder.SetWhereBinaryOp(XmlWriter writer, EdmParserNode parseNode)\r\n   at Microsoft.SharePoint.SPListItemEntityCollectionCamlQueryBuilder.SetViewQuery(SPQuery query, XmlWriter writer, StringBuilder sb)\r\n   at Microsoft.SharePoint.SPListItemEntityCollectionCamlQueryBuilder.BuildCamlQuery()\r\n   at Microsoft.SharePoint.SPListItemEntityCollectionCamlQueryBuilder..ctor(SPList list, RESTfulQuery restQuery, Nullable`1 itemId)\r\n   at Microsoft.SharePoint.SPListItemEntityCollection.TryWriteAsOData(ODataWriter writer, RESTfulQuery query, ProxyContext proxyContext)\r\n   at Microsoft.SharePoint.Client.ServerStub.Write(Object value, Uri path, ODataWriter writer, RESTfulQuery query, ProxyContext proxyContext)\r\n   at Microsoft.SharePoint.Client.Rest.RestRequestProcessor.Process()\r\n   at Microsoft.SharePoint.Client.Rest.RestRequestProcessor.ProcessRequest()\r\n   at Microsoft.SharePoint.Client.Rest.RestService.ProcessQuery(Stream inputStream, IList`1 pendingDisposableContainer)","internalexception":{"message":"The field 'EventDate' of type 'DateTime' cannot be used in the query filter expression.","type":"Microsoft.SharePoint.Client.InvalidClientQueryException","stacktrace":""}}}}

Is it possible somehow to use fields type of DateTime in filter query expressions?

Update: I found that I can do this via ListData.svc, e.g.

/_vti_bin/ListData.svc/Calendar?$filter=StartTime+ge+datetime'2011-11-23'

More details here - http://itblog.wolthaus.net/2011/12/rest-filter-datetime/

I tried to remove the time part from the /_api/web/ query, e.g.

/_api/web/lists/getbytitle('calendar')/items?$filter=( EventDate  ge  '2013-01-26')&$select=Title,EventDate,ID

But it didn't help and I got the same error.

These queries below don't work:

Query 1:

_api/web/lists/getbytitle('calendar')/items?$filter=( EventDate  ge datetime'2013-01-26T22:00:00Z')&$select=Title,EventDate,ID

OR

/_api/web/lists/getbytitle('calendar')/items?$filter=( EventDate  ge '2013-01-26')&$select=Title,EventDate,ID

Error:

The field 'EventDate' of type 'DateTime' cannot be used in the query filter expression

Query 2:

_api/web/lists/getbytitle('calendar')/items?$filter=( EventDate  ge datetime'2013-01-26')&$select=Title,EventDate,ID

Error:

The expression "( EventDate ge datetime'2013-01-26')" is not valid
  • SharePoint version please? – Robert Kaucher Apr 12 '13 at 19:01
  • Did you get the answer for the above question? – user41667 Apr 22 '15 at 10:04
  • Your comment is correct, this worked for me for date range. /_vti_bin/ListData.svc/Calendar?$filter=(StartTime+le+datetime'myStartDate' and EndTime+ge+datetime'myEndDate') Where myStartDate and myEndDate are the variables for the current item's eventDate and endDate. This returns only items that take place at the same time as the current item. – AtariPixel Oct 28 '18 at 10:49
1

You actually have the answer in your question. In your update you show:

/_vti_bin/ListData.svc/Calendar?$filter=StartTime+ge+datetime'2011-11-23'

Notice the string with the date is prefaced by datetime. You just need to do the same thing in the query you are trying to write.

http://www.odata.org/documentation/overview#AbstractTypeSystem

  • no, it doesn't work for /_api/web query. added these queries to my question. thank you! – Sergey Feb 13 '13 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.