3

I have to query from an Announcement list using REST. I want to get top 1 item whose field "Expires" (date value when i created an announcement) is greater than current date time.

Query:

/_api/web/lists/getbytitle('WF1Announcements')/Items?$select=ID,Title&$orderby=Expires&$filter=Expires geq ??? &$top=1

what should be put inplace of ???

  • You are trying to run before you can walk. First learn how to call REST endpoints and make it work with a hardcoded Date-string, then replace the hardcoded datestring with a dynamic one. If you don't then you are taking too large a steps (which you can't make because you don't have the skills yet) and it will take you a lot longer... or you copy/paste code without learning what it does – Danny '365CSI' Engelman Apr 6 '17 at 9:31
  • i am aware of REST and used it earlier. My query is working fine but having issue only with date filter condition, when i use &$filter=Expires geq ??? – MJaiswal Apr 6 '17 at 9:37
  • I used hardcoded value and it worked but had issue when using dynamic – MJaiswal Apr 6 '17 at 9:49
  • 1
    Then ask a clear question. Show us what you did the hardcoded way and show us what you tried that didn't work. And leave of the start of the REST call, we now have to scroll to see what you are after. Clear questions get you clear answers. Note: Your question has nothing to do with REST, you don't know how to format a Date string... – Danny '365CSI' Engelman Apr 6 '17 at 10:50
2

try this

...$filter=StartDate ge datetime'" + today.toISOString() + "' and ...

check this link REST API filter by start and end dates using 'today' and also Filtering Items by Date Range using SharePoint 2013 REST API

  • i have seen this example already and it did not work. Could you please reply in context of my question. – MJaiswal Apr 6 '17 at 9:14
2

Hi you need to provide a range for date filter to work in rest, below is example,

/_api/web/lists/getbytitle('WF1Announcements')/Items?$select=ID,StartDate&$orderby=StartDate &$filter=StartDate  ge datetime'2017-04-06T11:41:12.287Z' and StartDate  le datetime'2017-04-06T11:41:12.287Z'

You adjust le and ge according to your need.

  • you can replace the hard code value with today.toISOString() – Sunil Sahu Apr 6 '17 at 13:00
0

Try this.

var today = new Date();
var d = today.toISOString().substring(0,10) + "T00:00:00";

/_api/web/lists/getbytitle('WF1Announcements')/Items?$top=1$select=ID,Title&$filter=(Expires ge datetime'" + d + "')&$orderby=Expires desc"

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.