5

I have seen a lot of examples and questions about this out there but nothing I have found has helped me accomplish this. I need to be able to grab all the names of the documents within a given library, including all those that may exist within subfolders recursively.

Here is what I have:

 function documentQuery(listName) {
    var ctx = new SP.ClientContext.get_current();
    var oLibDocs = ctx.get_web().get_lists().getByTitle(listName);
    var caml = new SP.CamlQuery();
    caml.ViewXml = "<View Scope='RecursiveAll'></View>";
    this.allDocumentsCol = oLibDocs.getItems(caml);
    ctx.load(this.allDocumentsCol, "Include(FileLeafRef, ServerUrl)");

    ctx.executeQueryAsync(Function.createDelegate(this, this.onSucceededCallback), Function.createDelegate(this, this.onFailedCallback));
}

function onSucceededCallback(sender, args) {
    var libList= "";
    var ListEnumerator = this.allDocumentsCol.getEnumerator();

    while(ListEnumerator.moveNext())
     {
        var currentItem = ListEnumerator.get_current();
        var currentItemURL = domURL + currentItem.get_item('ServerUrl');
        libList += currentItem.get_item('FileLeafRef') + ' : ' + currentItemURL + '\n';
     }
     alert(libList);
 }

function onFailedCallback(sender, args) {
 alert("failed. Message:" + args.get_message());

}

so this works for all the first level documents, but also returns folder names. I thought the

I have tried RecursiveAll instead, I have tried placing it in ViewAttributes, I have tried having Recursive within View and adding FilesOnly to ViewAttributes to try to at least eliminate the folder names. none of this has worked.

so the basic thing i am looking to do is get the above idea (or something like it) to work by checking all items recursively (including all sub folders), but also return (alert in this case) only those that are files. this is ECMA/JSOM as I only have access to SPD.

any help with this is appreciated as always.

UPDATE: so SPServices works by adding the following to GetListItems:

CAMLQueryOptions: '<QueryOptions><ViewAttributes Scope="Recursive"/></QueryOptions>',

SPServices is awesome and I use if frequently, but there are certain cases where I need async operations and this is one of them. I have since tried modifying the CAML in the COM example but I can't seem to get it right to replicate this....any ideas?

1 Answer 1

2

A "workaround" is maybe to add an ContentType check in your ListEnumerator.moveNext()

If you change the following:

ctx.load(this.allDocumentsCol, "Include(FileLeafRef, ServerUrl, ContentType)");

And:

 while(ListEnumerator.moveNext())
     {      
        var currentItem = ListEnumerator.get_current();
        var ct = currentItem.get_contentType();
        if(ct.get_name() != "Folder") {
            var currentItemURL = domURL + currentItem.get_item('ServerUrl');
            libList += currentItem.get_item('FileLeafRef') + ' : ' + currentItemURL + '\n';
        }
     }

Update:

Ok, I had to test this myself :) Used createAllItemsQuery() and add a Where ContentType not equals Folder in the caml. This approach should work:

function documentQuery() {
    var ctx = new SP.ClientContext.get_current();
    var oLibDocs = ctx.get_web().get_lists().getByTitle("Dokumenter");
    var caml = SP.CamlQuery.createAllItemsQuery();
    caml.set_viewXml("<View Scope='RecursiveAll'><Query><Where><Neq><FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value></Neq></Where></Query></View>");
    this.allDocumentsCol = oLibDocs.getItems(caml);
    ctx.load(this.allDocumentsCol, "Include(FileLeafRef, ServerUrl)");
    ctx.executeQueryAsync(Function.createDelegate(this, this.onSucceededCallback), Function.createDelegate(this, this.onFailedCallback));
}

function onSucceededCallback(sender, args) {
    var libList= "";
    var ListEnumerator = this.allDocumentsCol.getEnumerator();

    while(ListEnumerator.moveNext())
     {      
        var currentItem = ListEnumerator.get_current();
        var currentItemURL = _spPageContextInfo.webServerRelativeUrl + currentItem.get_item('ServerUrl');
        libList += currentItem.get_item('FileLeafRef') + ' : ' + currentItemURL + '\n';
     }
     alert(libList);
 }

function onFailedCallback(sender, args) {
 alert("failed. Message:" + args.get_message());

}
3
  • thanks Anders...this works for eliminating the folders in mixed examples where there are folders and documents both at the root. my main problem is I can get the above to work with recursion....but still a step closer. appreciated!
    – Justin
    Jun 10, 2013 at 18:12
  • thanks so much Anders....guess I just couldn't get the query exactly right, needed to remove new keyword and change caml.ViewXml to set_viewXml. really appreciate the help!
    – Justin
    Jun 10, 2013 at 20:35
  • Np. Actually new SP.CamlQuery works too. Dunno why I changed it, guess I did some typos when testing :) Jun 10, 2013 at 21:24

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.