I am making app that display document library in nested list means if there is folder and it contain files then it displayed in nested files list.

Here is my code to get folders name inside document library.

   function getQueryStringParameter(param) {
    var params = document.URL.split("?")[1].split("&");
    //var strParams = "";    
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == param) {
            return singleParam[1];
function ViewAllFiles()
    var hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
    var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
    var context;
    var web;
    var factory;
    var appContextSite;
    var collList;
    context = new SP.ClientContext(appweburl);
    //factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
    appContextSite = new SP.AppContextSite(context, hostweburl);
    this.web = appContextSite.get_web(); //Code to get the cross domain
    var list = this.web.get_lists().getByTitle('Documentssssssssss');
    var query = SP.CamlQuery.createAllItemsQuery();    
    allItems = list.getItems(query);
    context.load(allItems, 'Include(Title, ContentType, File , Folder)');
    context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
function success()
    var foldername = "";
    var ListEnumerator = this.allItems.getEnumerator();
        var currentItem = ListEnumerator.get_current();
        var _contentType = currentItem.get_contentType();
        if(_contentType.get_name() == 'Folder')
            var fol= currentItem.get_folder();
            foldername += fol.get_name();
function failed(sender, args) {
    alert('failed. Message:' + args.get_message());
$(document).ready(function () {

This throws error. Is there any example available to get folder name and get file insight that folder?

1 Answer 1


For get files from folders you should need to load collection by passing folderPath through CamlQuery. Please refer below code:

var query = new SP.CamlQuery();
var Folderitems = list.getItems(query);
context.load(Folderitems, 'Include(Title, ContentType, File , Folder)');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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