Forum Discussion
Get top 3 files from Folder in SharePoint 2013 using REST api
I have an issue similar to Rishi Gupta. My project is to create a news article slider carousel that displays the eight most recent articles. My query works except for OrderBy=ArticleStartDate%20desc. When attempting to order by the article date the error is:
The desired query is http://XXXXXXXXXX.XXX/news/_api/web/GetFolderByServerRelativeURL('Pages/articles')/Files?$Expand=ListItemAllFields&$top=8&$orderby=ArticleStartDate%20desc. I've also tried orderby=ListItemAllFields/ArticleStartDate%20desc.
- Rishi GuptaJun 11, 2018Brass Contributor
It seems that this works on SharePoint Online, but does not work in SP 2013 on premise version.
There are two options to return the data:-
First option is to use getitems instead of GetFolderByServerRelativeURL see below query and it works fine. The only issue with this which I found is that in case your folder have special charachter's like <'> then it will give an error stating that its an invalid query.
http://sp13/sites/test1/_api/web/lists/getbytitle('Doc_anthour')/items?$select=ID,FileRef,Created,Author/ID&$expand=Author&$orderby=ArticleStartDate desc&$filter=substringof('Folder URL',FileRef)
Second option is use Post method and use REST + CAML query together
REST API query :- http://siteurl/_api/web/lists/GetByTitle('LibraryName')/GetItems?$select=*,Author,FileRef,FileLeafRef,FieldValuesAsText/Author&$expand=FieldValuesAsText
CAML Query :- "<View Scope='RecursiveAll'><Query><Where><And><Neq><FieldRef Name='ContentType'/><Value Type='Text'>Folder</Value></Neq><Eq><FieldRef Name='FileDirRef'/><Value Type='Lookup'>/Folder URL</Value></Eq></And></Where><OrderBy><FieldRef Name='ArticleStartDate' Ascending='False'/></OrderBy></Query><RowLimit>1</RowLimit></View>"
I used second option and it worked for me. Hope this will work for you as well.
- Darian GloverJun 15, 2018Copper Contributor
Hi Rishi Gupta,
Thanks for the reply. I figured out a simpler solution. The following query reads in articles from the "Pages" library within the folder "articles" and sorts them by ArticleDate in descending order. For the library I am working with, the display name of the ArticleStartDate column was changed to "Article Date".
Oh the fun with internal names, display names, and spaces.
/_vti_bin/Listdata.svc/Pages?$select=Title,Name,ArticleDate,Id,Path&$filter=endswith(Path,%20%27articles%27)&$orderby=ArticleDate%20desc
- Rishi GuptaJun 15, 2018Brass Contributor
I believe instead of listdata.svc, you can use REST api calls. They will be faster. Listdata.svc is old fashioned way of doing it.