1

I am trying to create a function which should loop through all lists on current web and display their current item count.

My code so far:

<!-- language: lang-js -->

ZN.updateDocumentCount = function () {
    var context = new SP.ClientContext.get_current();
    this.spLists = context.get_web().get_lists();

    context.load(this.spLists, "Include(DefaultDisplayFormUrl)");
    context.executeQueryAsync(Function.createDelegate(this, ZN.updateDocumentCount.Success), Function.createDelegate(this, ZN.updateDocumentCount.Failure));
}

ZN.updateDocumentCount.Success = function () {
    var enumerator = this.spLists.getEnumerator();
    while (enumerator.moveNext()) {
        var list = enumerator.get_current();
        var count = list.get_itemCount(); // <-- I get an error here
        alert(count);
    }
    SP.UI.Status.addStatus("Success", "All document counts are up-to-date.", false);
};

When the script gets to the part where it should get list.get_itemCount() it throws the following 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.

How can I otherwise achieve getting item counts for all lists?

3 Answers 3

1

Change context.load(this.spLists, "Include(DefaultDisplayFormUrl)"); to context.load(this.spLists); I checked all work correct in this case.

1

You need to load the idividual lists and then use execute query async just like you did in the first step when u loaded the whole list collection

1

You need to add the ItemCount field to the Include statement:

var context = SP.ClientContext.get_current();
var lists = context.get_web().get_lists();

context.load(lists, "Include(Title, ItemCount)");
context.executeQueryAsync(success, fail);

function success() {
    var message = "List item counts:";
    var enumerator = lists.getEnumerator();
    while (enumerator.moveNext()) {
        var list = enumerator.get_current();
        var title = list.get_title();
        var count = list.get_itemCount();

        message += "<br/>";
        message += String.format("{0}: {1}", title, count);
    }

    jQuery("#message").html(message);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.