Forum Discussion
Get top 3 files from Folder in SharePoint 2013 using REST api
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.
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.