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.

  • Try changing clientContext.load(oList); to clientContext.load(this.oList); – Amal Hashim Oct 30 '14 at 17:55
  • @AmalHashim I'd try clientContext.load(sender.oList); – Renan Oct 30 '14 at 17:59
  • Nope still same problem. Could it be because of how I call ExecuteOrDelayUntilScriptLoaded(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:08
  • Weird, it works when I use your code in IE dev tools. We can tell by the error that the JSOM library is loaded, so I don't think it's the way you're calling the function. I can only think of two things: 1) Try clientContext.load(oList, "Include(ItemCount)"); instead (not 100% sure that'll work). 2) Double check that no other code is using oList. There's a chance that something else is overwriting the variable after your async function is called, thus causing it to be uninitialized. Though the this.oList should prevent that from being the case. – wjervis Oct 30 '14 at 19:17
  • On a second note, must you use JSOM, or can you use REST? The endpoint for getting the item count would be <siteUrl>/_api/web/lists/getbytitle('Classification')/itemcount. – wjervis Oct 30 '14 at 19:18
1

You have some scoping problems...

When you say clientContext.load(oList);

The oList that you're referring to is different than the variable you set above using the 'this' keyword.

Either use this.oList throughout every call (this will work on the success and fail methods because of Function.createDelegate) or change the first this.oList to var oList = ... -- which will also still work in the success and fail methods because of Function.createDelegate

Here is my attempt at a fairly straight-forward re-factor:

    // note: I changed the function name -- js convention is Proper case for Constructors only
var customAction = function() {
        var clientContext = SP.ClientContext.get_current();
        var web = clientContext.get_web();
        var 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(onSuccess, onFail);

        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 handlers are defined inside of the calling function, which allows you to forego mucking around with 'createDelegate' or 'this'

  • Thank you for this. The problem I was actually having was that I was including sp.js inside the script which is why it worked in console but not the file. It must have caused wonkyiness problems with ExecuteOrDelayUntilScriptLoaded() – user3643344 Oct 31 '14 at 20:11
0

Try this

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', CustomAction);
  • Nope, no good either :/. I appreciate the help you are giving though. – user3643344 Oct 30 '14 at 19:04
0

To do this with REST, you can do the following:

var CustomAction = function(){
    var oDataUrl = hostUrl + "/_api/Web/Lists/GetByTitle('Classification')/ItemCount";
    $.ajax({
        url: oDataUrl,
        type: 'GET',
        headers: {
            accept: 'application/json;odata=nometadata',
            content-type: 'application/json;odata=nometadata'
        },
        success: onSuccess,
        error: onFail
    });

    function onSuccess(data) {
        alert('No of rows: ' + data.value);
    }

    function onFail(data, errCode, errMessage) {
        alert('Request failed.\n' +  errMessage);
    }
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.