6

I want to fetch ID and Title of last created item from SharePoint list using REST. I am using below url but it gives me wrong item, there are more than 10k items in the list.

http://servmachine.contoso.com/_vti_bin/ListData.svc/Products?OrderBy=ID%20DSC&$top=1

SPArcheon
  • 6,868
  • 1
  • 30
  • 47
SBP
  • 337
  • 6
  • 16

1 Answers1

9

For applying orderby and top you need to call it like this

http://servmachine.contoso.com/_vti_bin/ListData.svc/Products()?$top=1&$orderby=Id desc

MSDN

Amal Hashim
  • 28,306
  • 5
  • 31
  • 61
  • Sandip might want to ordered by Created or Modified instead of ID. – James Grizzle Nov 21 '14 at 15:56
  • Once you put parenthesis Products() it can be ordered by any field. – Amal Hashim Nov 21 '14 at 16:10
  • 1
    @AmalHashim I think you need ID instead of Id. @james-grizzle why would Created be better than ID, wouldn't it be the same? And Modified would not be the last created item. – eirikb Nov 21 '14 at 20:48
  • @eirikb is correct. – James Grizzle Nov 21 '14 at 21:00
  • @AmalHashim, parenthesis are not mandatory and @eirikb it is Id and not ID. FYI, orderby doesn't work if top is not used. See here: http://sharepoint.stackexchange.com/questions/58188/i-cannot-get-orderby-to-work-in-sps-2010-listdata-svc-rest-query – Nadeem Yousuf-AIS Nov 24 '14 at 06:53
  • Ok @NadeemYousuf. I just did a test on SharePoint Online and there ID worked, while Id did not. Could be difference between 2010 and 2013? Beside that in my test $orderBy combined with $top also worked. – eirikb Nov 24 '14 at 06:57
  • @eirikb I am on SP 2013 on premise and it is opposite here. ID doesn't work. $orderBy works when used with $top. However, if $orderBy is used alone it doesn't work – Nadeem Yousuf-AIS Nov 24 '14 at 07:01