SharePoint: Get Files from folder using JavaScript client object model

The example below uses SP.CamlQuery() to get items by setting set_folderServerRelativeUrl . 
<script type="text/javascript"> 
    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());
    }
</script>
The other way of getting the files inside folder is to use get_files() method of Folder
<script type="text/javascript">
    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());
    }
</script>
profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+