1

I am using API call to fetch news items from a custom list, and filter by its date and then orderby news item's title. However orderby clause never works, if I put $orderby before $filter, then filter clause stop working.

Here is my CAML query:

/_api/web/lists(guid'CE7A5EC9-D264-48D9-8F81-1C2F8C7341XX')/items?
$select=ID,Title,StartDate,Expires
&$filter=StartDate le datetime' + midnightDayIsoString + ' and Expires ge datetime' + midnightDayIsoString + '
&$orderby=Title

midnightDayIsoString is defined by using new Date() function, calling .toISOString() method, and replace the time parts with hardcoded 12:00:00Z. e.g. datetime('2017-08-15T12:00:00Z')

API call is using ajax:

$.ajax({
            url:  siteurl + query,
            method: "GET",
            headers: {"Accept": "application/json; odata=verbose"},
            RowLimit: null,     
            RowsPerPage: null,  
            success:onSuccess,
            complete:onComplete,
            error:onError
        });

The query returns correct filtered result, but does not do the $orderby part.

I don't know that why orderby is not working. I need some help on this. Thanks.

  • I am using SharePoint 2013. – Alan Aug 15 '17 at 0:24
  • are you missing the ? between ../items?$select=... – Mike Aug 15 '17 at 0:29
  • Sorry that was my typo when I was entering the question, I will correct that. Thanks for pointing it out. – Alan Aug 15 '17 at 0:34
  • Can you show us your entire code including how you are deriving midnightDayIsoString? – Mike Aug 15 '17 at 1:04
  • I've just update the post with some code sample in there, actually quite standard. – Alan Aug 15 '17 at 1:40
2

If you have the dates already as an ISO string, you don't need the datetime parser in REST.

Try:

var query = "/_api/web/lists(guid'CE7A5EC9-D264-48D9-8F81-1C2F8C7341XX')/items?
$select=ID,Title,StartDate,Expires
&$filter=StartDate le '" + midnightDayIsoString + "' and Expires ge '" + midnightDayIsoString + "'
&$orderby=Title";
1

Try with

&$orderby=Title asc
or &$orderby=Title desc

If you didn't get expected results then try without filter in query like as bellow.

/_api/web/lists(guid'CE7A5EC9-D264-48D9-8F81-1C2F8C7341XX')/items?
$select=ID,Title,StartDate,Expires&$orderby=Title

If you got results in sorting order then you need check filter condition in URL.

Hope this helps you.

  • thanks for your answer. I tied this before, it worked. It was a bit strange when i put both $filter and $orderby parameter, then the results didn't get sorted. But just minutes ago, it starts working that I cannot explain why worked. – Alan Aug 15 '17 at 4:46

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.