Forum Discussion

Rishi Gupta's avatar
Rishi Gupta
Brass Contributor
Jun 07, 2018

Get top 3 files from Folder in SharePoint 2013 using REST api

I am currently working on a query to get top 3 files from SharePoint document library which has folders within a folders structure. I am currently using below query 

http://siteurl/_api/web/GetFolderByServerRelativeUrl('libraryname/folder1/folder2')/Files?$expand=ListItemAllFields,Author/ID,ListItemAllFields/FieldValuesAsText,ListItemAllFields/Published_x0020_Date&$orderby=ListItemAllFields/Published_x0020_Date%20desc&$top=3

The above query works fine without $orderby. When I add $orderby it throws below exception 

<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code>
-1, Microsoft.SharePoint.Client.InvalidClientQueryException
</m:code>
<m:message xml:lang="en-US">
Field or property "Published_x0020_Date" does not exist.
</m:message>
</m:error>

 

Does anyone has any idea why this is not working?

 

Any help is appreciated.

4 Replies

  • Darian Glover's avatar
    Darian Glover
    Copper Contributor

    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:

     

    <m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code>
    -1, Microsoft.SharePoint.Client.InvalidClientQueryException
    </m:code>
    <m:message xml:lang="en-US">
    Field or property "ArticleStartDate" does not exist.
    </m:message>
    </m:error>
     
    Without the OrderBy the returned information includes the ArticleStartDate information, so it clearly exists. See screen captures below.
     

     

     

    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 Gupta's avatar
      Rishi Gupta
      Brass Contributor

      Hi Darian Glover 

       

      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 Glover's avatar
        Darian Glover
        Copper 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

Resources