0

I'm no stranger to this error or the various solutions, but this one has me scratching my head. I'm using JavaScript object model to get all a list of all the files in a given folder. I get the error on the getEnumerator in the code below. I've stripped the code down to the bare minimum:

function getFilesInFolder() { 
    var folderServerRelativeUrl = folderPath + ID;
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle(documentLibraryName);
    var query = SP.CamlQuery.createAllItemsQuery();
    query.set_folderServerRelativeUrl(folderServerRelativeUrl);

    //Update "web part" link
    $("#doclink").attr('href',folderServerRelativeUrl);
    files = list.getItems(query)
    context.load(files, 'Include(Id)');
    context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFailure));
}

function OnSuccess()
{       
//ERROR Next Line:                                   
    var listItemEnumerator = this.files.getEnumerator();
    var table = $("#attachments");


    while (listItemEnumerator.moveNext())  
    {                                    
        console.log("Found a file");
    }                                         
}

The code is called as such in the beginning of the file: $(document).ready(function(){ //Other code... ExecuteorDelayUntilScriptLoaded(getFilesInFolder,"sp.js"); });

I've tried a ton of variations on this AND it used to work (not sure what changed either server or client-side).

2 Answers2

1

I tried your code, since I couldnt see any obvious errors, and it works. I hardcoded the folderServerRelativeUrl as it works in Swedish: "/sites/intranet/dokument" is my root web and the "Documents" folder.

You can try in the broswer "sitecollection/_api/web/getFolderByServerRelativeUrl('/path/to/folder/')" To see if the url u are using is a correct one.

You could also set a breakpoint in your onsuccess and look in the console: files.get_count() to see if you have any results.

Your load is fine, so dont worry about that!

function getFilesInFolder() { 
        var folderServerRelativeUrl = "/sites/intranet/dokument";
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle("Dokument");
        var query = SP.CamlQuery.createAllItemsQuery();
        query.set_folderServerRelativeUrl(folderServerRelativeUrl);

        //Update "web part" link
       // $("#doclink").attr('href',folderServerRelativeUrl);
        files = list.getItems(query)
        context.load(files, 'Include(Id)');
        context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFailure));
    }

    function OnSuccess()
    {       
    //ERROR Next Line:                                   
        var listItemEnumerator = this.files.getEnumerator();
        var table = $("#attachments");


        while (listItemEnumerator.moveNext())  
        {                                    
            console.log("Found a file");
        }                                         
    }
simon
  • 140
  • 2
  • 11
  • Thanks for confirming, I wasn't sure if I was going insane. :) I should have mentioned that I did confirm that the path was correct. I think Vadim may be correct that I have scope problems. I'll let you know what the result is! – Peter Joyce Mar 16 '16 at 15:21
1

This error basically means that resulting object (this.files variable) has not been requested from the server once the iteration is performed. This error would be reproduced by executing simultaneously several queries (basically invoking getFilesInFolder several times).

Given the fact that result object (this.files) is stored in global scope (window object) i would strongly suggest against this approach of getting list items, instead you could consider to store the result in local scope as demonstrated below:

function getFilesInFolder() { 
    var folderServerRelativeUrl = "/Documents/Archive";
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle(documentLibraryName);
    var query = SP.CamlQuery.createAllItemsQuery();
    query.set_folderServerRelativeUrl(folderServerRelativeUrl);
    var items = list.getItems(query)
    context.load(items, 'Include(Id)');
    context.executeQueryAsync(
    function(){
       if(items.get_count() > 0)
       {                                   
           console.log('Found a file(s)');
       }                   
    }, 
    function(sender,args){
        console.log(args.get_message());  
    });
}

With such approach you could easily avoid conflicts with overriding resulting object and therefore getting this exception.

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • 1
    Thanks, I'll give that a try. I am getting inconsistent results now, so I suspect that it may be something along those lines. I'm not using "files" anywhere else in my code, but that doesn't mean some other library isn't using it, good thought, thanks! I'll let you (and the world) know if it works. FYI, the pattern I used comes from Microsoft's examples -- they really should update to something more like what you ahve a bove to avoid those scope problems. – Peter Joyce Mar 16 '16 at 15:15
  • if still didn't work, I ended up using SPServices, which I'm trying to get away from. I suspect it has something to do with this specific site or library, I'm going to investigate further later, but I had to get this pushed out the door. – Peter Joyce Mar 17 '16 at 15:11