Filter REST call on SharePoint with DateTime value

As you may know, using the REST API in SharePoint 2010 is a very flexible and powerful way to retrieve data from SharePoint. I love the way I can use the ListData.svc to get data form lists and document libraries. Finding the correct syntax however can sometimes be challenging…

Today, I found myself looking for a way of retrieving items from a calendar list. Getting the calendar items themselves wasn’t a problem, try to filter the items for only the current items (today and greater) was.

After some Googling and several syntax attempts I found out the right way to do it.
Say I only want to  get calendar items that start  on November 23, 2011 or beyond…

http://<servername>/_vti_bin/ListData.svc
/Calendar?$filter=StartTime+ge+datetime'2011-11-23T00:00:00'

Works like a charm…

Update March 27, 2012
Because of the question Michal asked I retested some scenario’s. It turns out you cannot filter on time values. Real bummer :(
So:

http://<servername>/_vti_bin/ListData.svc
/Calendar?$filter=StartTime+ge+datetime'2011-11-23T09:00:00'

will filter on dates, greater or equal than 2011-11-23. It will not filter on 09:00, so an event taking place at i.e. 08:00 will be returned in the result set.

Therefore, there is no need to specify the time and you can filter without the time part of the query, like this:

http://<servername>/_vti_bin/ListData.svc
/Calendar?$filter=StartTime+ge+datetime'2011-11-23'

If I’m wrong, please let me know…

 

This entry was posted in SharePoint 2010. Bookmark the permalink.

6 Responses to Filter REST call on SharePoint with DateTime value

  1. Caspar says:

    Does it work for recurring events? Just asking…
    If so, you’ve landed another job. :D

  2. Ernst Wolthaus says:

    Caspar,
    Unfortunately, this will not retrieve the recurring calendar items…
    If you want to get recurring calendar items with client side code, I suggest you use SPServices. Please take a look at this thread: http://spservices.codeplex.com/discussions/254560

  3. Michal Hainc says:

    hi, for me this does not return correct results
    http:////_vti_bin/listdata.svc/Calendar()?$filter=(StartTime%20ge%20datetime’2012-03-22T00%3A00%3A00%2B01%3A00′)%20and%20(StartTime%20le%20datetime’2012-03-22T09%3A00%3A00%2B01%3A00′)&$select=StartTime,EndTime

    it returns an appointment starting on 22.03.2012 16:00 and ended 17:00

    • Ernst Wolthaus says:

      Hi Michal,

      Good question! I’ve tested your filter string and it turns out you can filter with dates but NOT with time. Real bummer :(

      I have updated the blog post accordingly.

      • Michal Hainc says:

        Hi Ernst thank you for updating the blog post, … yes… it is a bummer indeed…
        and I feel like there is million of such bummers hidden in the sharepoint monster… :(

        going to think about a workaround

  4. Pingback: SharePoint 2013 et l’agrégation de données provenant du SI puis exposition à une application mobile | SharePoint 2013 - Blog technique d'Alexandre DAVID

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>