0

REST get all subfolder and files in one call, limiting the fields returned and sort by folder or file name.

I currently have a URL at:

https://domain.sharepoint.com/sites/subsite/_api/Web/GetFolderByServerRelativeUrl('/sites/subsite/Shared%20Documents/subfolder')?$expand=Files,Folders/Files,Folders/Folders/Files,Folders/Folders/Folders/Files,Folders/Folders/Folders/Folders/Files,Folders/Folders/Folders/Folders/Folders/Files,Folders/Folders/Folders/Folders/Folders/Folders/Files&$select=Name,Files,Folders,ServerRelativeUrl&$orderby=Name&$select=Name

which returns all 500 subfolders with their 2,000 files. The problem is that this response is 20 megabytes and includes all the columns of data for each file and folder. They are also not in alphabetical order within a folder.

I've tried using the $orderby and $select, but these have no impact on the resonse. Is there a way to limit the fields of data returned to just the name and to have the results within a folder to be sorted alphabetically?

I'm using JavaScript to parse the JSON into a file tree structure and this works fine. The problem is that it takes 20 seconds to receive such a large response (most of the data columns are useless for me) and results are not ordered alphabetically. I could order the results using JavaScript, but if there is a handy way to do this in the REST call, then even better. Thanks!

1 Answer 1

0

I was able to drastically reduce the amount of metadata contained in the response by adding the below header to my call:

accept: application/json;odata=nometadata

My response size went from 20 megabytes to 4 megabytes.

For sorting the folders and files alphabetically, I used:

filesarray.sort(function(a,b){
    if(a.Name < b.Name) { return -1;}
    if(a.Name < b.Name) { return 1; }
});

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.