1

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);
    context.set_webRequestExecutorFactory(factory);
    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();
    while(ListEnumerator.moveNext())
    {
        var currentItem = ListEnumerator.get_current();
        var _contentType = currentItem.get_contentType();
        if(_contentType.get_name() == 'Folder')
        {
            var fol= currentItem.get_folder();
            foldername += fol.get_name();
        }
    }
    $('#listall').append(fileUrls);
}
function failed(sender, args) {
    alert('failed. Message:' + args.get_message());
}
$(document).ready(function () {
    ViewAllFiles();
});

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

1 Answer 1

2

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();
query.set_folderServerRelativeUrl(folderPath);
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 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.