5

I am using a REST API to get the list of all files inside a document library.

However, this also brings the list of all folders.

Is there a way to filter out the folders and bring only the files using the REST API?

$.ajax({
        url: _spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getbytitle('Document Library Name')/items?$top=1000&$select=Title,FieldValuesAsText/FileRef,FieldValuesAsText/FileRef/Title&$expand=FieldValuesAsText",
        headers: { "Accept": "application/json; odata=verbose" },
        cache: false,
        success: function (data) {
            //some code
        },
        error: function (xhr) {
            console.log(xhr);
        }
    });
6

A few more options actually available:

items?$top=1000&$select=Title,FileLeafRef,FieldValuesAsText/FileRef,FieldValuesAsText/FileRef/Title&$expand=FieldValuesAsText&$filter=FSObjType eq 0

FSObjType (display name Item Type) is a built-in field, values are based on this enumeration

OR

items?$top=1000&$select=Title,FileLeafRef,FieldValuesAsText/FileRef,FieldValuesAsText/FileRef/Title&$expand=FieldValuesAsText&$filter=startswith(ContentTypeId, '0x0101')

Later approach uses content type and filters out only items which are File-based content type (0x0101)

  • Thanks! this approach worked for me, however, I didnt need to expand by "FieldValuesAsText", all I had was to filter by "FSObjType eq 0" – Alberto Jul 12 '19 at 10:24
2

you can use this snip of code to filter your results and exclude Folders

var g;  
$.ajax({  
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/Lists/GetByTitle('Training Material')/Items",  
type: 'GET',  
dataType: "json",  
headers: {  
    "Accept": "application/json;odata=verbose",  
    "content-type": "application/json; odata=verbose",  
    "X-RequestDigest": $("#__REQUESTDIGEST").val()  
},  
success: function (data) {  
    for (var i = 0; i < data.d.results.length; i++) {  
        if (data.d.results[i].FileSystemObjectType != 1) {            
        }  
    }  
},  
error: function (request, error) {  
    console.log(JSON.stringify(request));  
}  
}); 

The above code will return all the items of document library including folders. So, to exclude folders, we can use FileSystemObjectType property to determind whether the current item is folder or file. For more information on FileSystemObjectType, visit This Link

1

You have to change the REST URL and point to the document library. data.Files property will contain only the files and data.Folders will contain the folders.

var RestUrl= _spPageContextInfo.webAbsoluteUrl+ "/_api/Web/GetFolderByServerRelativeUrl('/Shared%20Documents')?$expand=Folders,Files";      //document library name

    $.getJSON(RestUrl,function(data,status,xhr){

        if(data.Files.length > 0)
        {
        var results = data.Files;
        }
    });

The above code is tested and works fine.

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.