1

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.

Display_Here
  • 311
  • 1
  • 10

1 Answers1

1

Try this instead

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(sender, args){alert('No of rows: ' + oList.get_itemCount())}(this, this.onSuccess)), (function(sender, args){alert('Request failed.\n' + args.get_message() + '\n' + args.get_stackTrace())}(this, this.onFailure)));
};
kei
  • 20,157
  • 2
  • 35
  • 62
  • That causes `Uncaught TypeError: Cannot read property 'propertyHasNotBeenInitialized' of undefined` but running in the console also causes the original error instead of this one. – Display_Here Oct 30 '14 at 17:14
  • 1
    hmmm... are you wrapping your function with `ExecuteOrDelayUntilScriptLoaded` by the way? – kei Oct 30 '14 at 17:16
  • I am, `ExecuteOrDelayUntilScriptLoaded(CustomAction, "sp.js")` Is it maybe because I have to say the directory of the file or does that not matter. – Display_Here Oct 30 '14 at 17:21
  • 1
    Oh the file is in a folder? That I'm not sure. I would suggest testing on a different list/library. I would also suggest asking this question in [SharePoint StackExchange](http://sharepoint.stackexchange.com) – kei Oct 30 '14 at 17:26
  • Sorry, what I said didn't make much sense. I meant that "sp.js" might not be pointing correctly but it does not seem to care about that so that's irrelevant. I have posted there though so thank you! – Display_Here Oct 30 '14 at 17:36