4

I need to retrieve pages that is in the file "Folder". Doc/Folder/

var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle('Doc');

When I do this it works but it doesn't go in my file, of course, because I didn't specify it. So when I do this:

var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle('Doc/Folder');

That doesn't work! I created my pages that I put in that Folder..

3 Answers 3

1

Here are the couple of ways of doing it. Taken from my blog post : Get Files from folder using JavaScript client object model using SP.CamlQuery.createAllItemsQuery()

var allItems;
function GetFilesFromFolder(listTitle, folderServerRelativeUrl)
{
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle(listTitle);
    // Use createAllItemsQuery to get items inside subfolders as well. Otherwise use new SP.CamlQuery() to get items from a single folder only
    var query = SP.CamlQuery.createAllItemsQuery();
    query.set_folderServerRelativeUrl(folderServerRelativeUrl);
    allItems = list.getItems(query);
    context.load(allItems, 'Include(File, FileSystemObjectType)');
    context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFailure));
}
function OnSuccess()
{                                              
    var listItemEnumerator = allItems.getEnumerator();
    while(listItemEnumerator.moveNext())
    {
        var currentItem = listItemEnumerator.get_current();                                                                                                                     
        if(currentItem.get_fileSystemObjectType() == "0")
        {
            var file = currentItem.get_file();
            if(file != null)
            {
                alert('File Name: ' + file.get_name() + '\n' + 'File Url: ' + file.get_serverRelativeUrl());                                                                                     
            }
        }
    }                                              
}

function OnFailure(sender, args) {
    alert("Failed. Message:" + args.get_message());
}

Using Folder.get_files();

    var files;
    function getFilesInFolder(folderServerRelativeUrl) {        
        var context = SP.ClientContext.get_current();
        var web = context.get_web();
        var folder = web.getFolderByServerRelativeUrl(folderServerRelativeUrl);
        files = folder.get_files();
        context.load(files);
        context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFailure));
    }
    function OnSuccess()
    {                                              
        var listItemEnumerator = files.getEnumerator();
            while (listItemEnumerator.moveNext()) {
                var fileUrl = listItemEnumerator.get_current().get_serverRelativeUrl();             
            }                                         
    }

    function OnFailure(sender, args) {
        alert("Failed. Message:" + args.get_message());
    }
0
1

Use the following function on your Ecmascript file. (Sharepoint 2010 or 2013)

 function getLibraryFiles(listName, url, callback){
    var web = clientContext.get_web();
    var list = web.get_lists().getByTitle(listName);
    var query = new SP.CamlQuery();
    query.set_folderServerRelativeUrl(url);
    var allItems = list.getItems(query);
    clientContext.load(allItems, 'Include(ContentType, File, FileLeafRef, ServerUrl)');     
    clientContext.executeQueryAsync(function(sender, args){
        var file, item, itemsEnumerator = allItems.getEnumerator();
        var result = new Array();
        while (itemsEnumerator.moveNext()) {
            item = itemsEnumerator.get_current();
            var r = {   
                Title: item.get_item('Title'),
                ContentType: item.get_item('ContentType'),
                File: item.get_item('File'),
                FileLeafRef: item.get_item('FileLeafRef'),
                ServerUrl: item.get_item('ServerUrl')
            };
            if(item.get_contentType().get_name() === 'Folder')
            {
                r.type='d';
                r.name=r.FileLeafRef
            } else {
                r.type='f';
                file = item.get_file();
                if(file!=null){
                    r.url=file.get_serverRelativeUrl();
                    r.name=file.get_name();
                    r.created=file.get_timeCreated();
                }
            }
            result.push(r);
        }
        callback(result);
    });
  }

The URL variable should be made dynamic if you wish to get documents from other library.

0

Use this code:

var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle('Doc');
var query = SP.CamlQuery.createAllFoldersQuery();
var folders = list.getItems(query);

Source: Get all Folders using Ecmascript\Javascript client object model SharePoint 2010

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.