1

I have a document library named DocLibrary. Inside that, I have created few folders. Each folder have 3 another folder. Inside that, I store the specific files. Example: DocLibrary > NF1> Communications, where DocLibrary is the document library, NF1 is the folder inside DocLibrary and Communications is the folder inside NF1.

Now I want to give the download link to a particular file inside the Communications folder sorted by modified date. I am facing problem on how to goto Communications folder and then select file name.

I am using JavaScript for that.

Right now I am using the below code. But I am completely new to this and have limited ideas on How to approach.

    function test1()
{
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle('DocLibrary');

    alert(list);
    var query = SP.CamlQuery.createAllItemsQuery();
    query.set_folderServerRelativeUrl('/DocLibrary/NF1/Communications/');
    allItems = list.getItems(query);
    alert('hi');
    context.load(allItems);

    context.executeQueryAsync(Function.createDelegate(this, this.success),   Function.createDelegate(this, this.failed));
}

function success()
{
    var fileUrls = '';
    var ListEnumerator = this.allItems.getEnumerator();
    while(ListEnumerator.moveNext())
    {
        var currentItem = ListEnumerator.get_current();
        var _contentType = currentItem.get_contentType();
        if(_contentType.get_name() != 'Folder')
        {
            var File = currentItem.get_file();
            if(File != null)
            {
                fileUrls += File.get_serverRelativeUrl() + '\n';
            }
        }
    }
    alert(fileUrls);
}
function failed(sender, args) {
    alert('failed. Message:' + args.get_message());
}
1

Try this

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle("DocLibrary");
var query = new SP.CamlQuery();
var caml = "<View><Query><OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy></Query></View>";
query.set_folderServerRelativeUrl("/DocLibrary/NF1/Communications/");
query.set_viewXml(caml);
var allItems = list.getItems(query);
context.load(allItems, 'Include(Title, File)');
context.executeQueryAsync(Function.createDelegate(this, this.success),   Function.createDelegate(this, this.failed));

function success() {
    var fileUrls = "";
    var ListEnumerator = allItems.getEnumerator();
    while(ListEnumerator.moveNext()){
        var currentItem = ListEnumerator.get_current();
        var File = currentItem.get_file();
        if(File != null)
            fileUrls += File.get_serverRelativeUrl() + '\n';              
        }
    }
    alert(fileUrls);
}

function failed(sender, args) {
    alert("failed. Message:" + args.get_message());
}
  • Thanx Amal. I tried that code. Its giving error : "Value doesnot fall withing expected range" – Mahatma Aladdin Apr 17 '15 at 13:20
  • in the last line of the function ie: context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed)); – Mahatma Aladdin Apr 17 '15 at 13:27
  • Remove this from this.success and this.failed – Amal Hashim Apr 17 '15 at 13:29
  • did that. now its like this: context.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, failed)); still the same error. – Mahatma Aladdin Apr 17 '15 at 13:35
  • Ok. Change query.set_folderServerRelativeUrl("/NF1/Communications/"); – Amal Hashim Apr 17 '15 at 13:38
0

You can get the value using REST service use following URL format

http://SiteURL/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/fol1/fol2')/Files?$orderby=TimeLastModified desc

function FetchAllFiles() {
$.ajax({
    url:"http://SiteURL/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/fol1/fol2')/Files?$orderby=TimeLastModified desc" ,
    type: "GET",
    headers: { "Accept": "application/json;odata=verbose" },
    async: false,
    success: function (data) {

    },
    error: function (data2) {

    }
});

}

  • Thanx Ravin. Can you give a sample example. Where I have to put this code? – Mahatma Aladdin Apr 17 '15 at 13:30
  • @MahatmaAladdin Sample code updated in answer – Ravin Singh D Apr 17 '15 at 13:34

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.