17
var restURLAuthors = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('GoGreenReport')/items?$select=EncodedAbsUrl,FileRef&$filter=Project_x0020_Name eq '"+$("#ddnProjectName").val()+"'";

i want to add order by descending order of items? Where and how to write order by in this code specifically?

1
  • The above code along with other code adds item of attachment of a form to a library, taking project name as primary key , but with same project name multiple attachments are getting saved and when viewed its showing the oldest one, i want to view the latest one?
    – OM-ॐ
    Mar 6, 2017 at 10:51

2 Answers 2

28

You should add order by in the REST query itself as below:

/_api/web/lists/getbytitle('GoGreenReport')/items?$select=EncodedAbsUrl,FileRef&$filter=Project_x0020_Name eq '"+$("#ddnProjectName").val()+"'&$orderby=Created desc

Here, i have appended $orderby=Created desc, you can also use $orderby=Modified desc or any other custom field with its internal name. In case if you want the latest one, you can also use $orderby=ID desc

2
  • I tried this whole code = var restURLAuthors = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('GoGreenReport')/items?$select=EncodedAbsUrl,FileRef&$filter=Project_x0020_Name eq '"+$("#ddnProjectName").val()+"'&$orderby=ID desc"; BUT this doesnt work, am i doing something wrong? @gautam sheth
    – OM-ॐ
    Mar 7, 2017 at 4:23
  • it works for me, i dont think what you are doing is wrong. Log the results in console and check Mar 7, 2017 at 10:04
2

Like the comments said, it is all done in the URI

SharePoint endpoints are ODATA endpoints

Search has different syntax

iREST

4
  • Thanks for the reply but i want specific answer to the code line as i have provided, if you could write me the script code itself it would be better.
    – OM-ॐ
    Mar 7, 2017 at 4:42
  • 1
    i just wanted one specific line of code . Well there's world full of people like you anyways
    – OM-ॐ
    Mar 7, 2017 at 11:23
  • That was a good compliment to yourself. i appreciate
    – OM-ॐ
    Mar 8, 2017 at 4:15
  • the SharePoint REST API Overview link is going to SharePoint Workflow fundamentals.. docs.microsoft.com/en-us/sharepoint/dev/general-development/… Aug 8, 2018 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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