The error in question
Uncaught Error: The property or field has not been initialized.
It has not been requested or the request has not been executed.
It may need to be explicitly requested.
Here is the code, just trying to get a simple count of a list.
var CustomAction = function(){
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
this.oList = web.get_lists().getByTitle("Classification");
// .load() tells CSOM to load the properties of this object
// multiple .load()s can be stacked
clientContext.load(oList);
// now start the asynchronous call and perform all commands
clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));
// method will exit here and onSuccess or OnFail will be called asynchronously
};
function onSuccess(sender, args) {
alert('No of rows: ' + oList.get_itemCount());
};
function onFail(sender, args) {
alert('Request failed.\n' + args.get_message() + '\n' + args.get_stackTrace());
};
The error occurs at oList.get_itemCount()
. What reason would there be for this to be happening? I tried using $( document).ready
and $(window).onload
but the problem still occurs.
So like I said, it works when I copy/paste that into the browser but running it from file it doesn't.
clientContext.load(oList);
toclientContext.load(this.oList);
– Amal Hashim Oct 30 '14 at 17:55clientContext.load(sender.oList)
; – Geeky Guy Oct 30 '14 at 17:59ExecuteOrDelayUntilScriptLoaded(CustomAction, "sp.js");
? There seems to be many different sp files running in the background. i.imgur.com/rZjFAge.png – user3643344 Oct 30 '14 at 18:08clientContext.load(oList, "Include(ItemCount)");
instead (not 100% sure that'll work). 2) Double check that no other code is usingoList
. There's a chance that something else is overwriting the variable after your async function is called, thus causing it to be uninitialized. Though thethis.oList
should prevent that from being the case. – wjervis Oct 30 '14 at 19:17<siteUrl>/_api/web/lists/getbytitle('Classification')/itemcount
. – wjervis Oct 30 '14 at 19:18