Reputation: 43
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).
Upvotes: 0
Views: 1103
Reputation: 59338
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.
Upvotes: 1
Reputation: 140
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");
}
}
Upvotes: 1