1

I want to count the list items using javascript

1

2 Answers 2

5

Use following code:

var oList;
function theFunction() {
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();
    var oList = web.get_lists().getByTitle("Absences");

    // .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);
    // 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());
}

You can also try : http://sharepointdotnetwiki.iblogger.org/2012/09/using-jquery-to-get-total-items-in-sharepoint-list/

1
  • You have a bug in that code, you can not redeclare oList again within theFunction, because then the global oList that onSuccess is accessing is different and empty.
    – Alex
    Dec 29, 2016 at 12:16
1

You got the answer with Microsoft API, also one with SPServices. Let me add an answer with my JavaScript library called SharepointPlus:

$SP().list("Your List Name").get({fields:"ID"}, function(data) {
  alert("Number of items: "+data.length)
})

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.

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