I have a set of folders say Folder1 containing Folder2 and Folder3. Folder3 contains Folder4, Folder5 and Folder6.

When i use the OData expand parameter - $expand=Folders/Folders i get only details about Folder 2 and Folder3. I also want to retrieve Folder4,Folder5 and Folder6 in the same call.

The current rest api call is as below


How do i mention that my scope should be recursive?

As per the attached discussion thread, the CAML query provisions setting the scope as recursive to fetch recursively. Please advise if there is the REST equivalent for the same!!


I think you should use CAML query with REST API.

Here you can find your solution on this answer by Vadim Gremyachev

Sample code provided by Vadim Gremyachev:

function getListItems(webUrl,listTitle, viewXml) 
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; 
    var queryPayload = {  
               'query' : {
                      '__metadata': { 'type': 'SP.CamlQuery' }, 
                      'ViewXml' : viewXml

    return $.ajax({
           url: url,
           method: "POST",
           contentType: "application/json;odata=verbose",
           data: JSON.stringify(queryPayload),
           headers: {
              "X-RequestDigest": $("#__REQUESTDIGEST").val(),
              "Accept": "application/json; odata=verbose"

function getAllFolderItems(webUrl,listTitle)
    var viewXml = '<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FSObjType" /><Value Type="Integer">1</Value></Eq></Where></Query></View>';
    return getListItems(webUrl,listTitle,viewXml);

Your query may like:

<View Scope="RecursiveAll">
               <FieldRef Name="FSObjType" /><Value Type="Integer">1</Value>
  • Thank you for this answer. All along I needed to add the scope to the view so I can get subdirectories. – Coding101 Mar 26 '20 at 21:34

_spPageContextInfo.webAbsoluteUrl + "/_api/Lists/getbyTitle('" + LibraryName + "')/Items?$select=FileLeafRef,FileRef,Id&$top=5000

Yes Folder Data is just giving you a Detail. But you need to Traverse through the URL so you can get Your Root folder. Above url will fetch your records recursively also. no need to expand Folder.

And you can see above Caml Query Example if you change Query var viewXml = ;

so result will be same as per my REST URL.

  • Changed Query For Trimantra Solution:<View Scope="RecursiveAll"></View> – Negi Rox Dec 6 '17 at 7:54

There is a different approach, in which you can just request for the items within a document library, and add a filter param:

/_api/lists/getbytitle('[your library]')/items?$filter=FSObjType eq 0
  • FSObjType eq 0 -> files
  • FSObjType eq 1 -> folders

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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