1

I want to count the list items using javascript

Prasanna
  • 11
  • 1
  • 1
  • 2
  • 1
    Possibly duplicate of http://sharepoint.stackexchange.com/questions/5477/getting-a-count-of-list-items-in-a-list-via-ecmascript?rq=1 – Vadim Gremyachev Jun 24 '14 at 12:05

2 Answers2

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/

Aanchal
  • 7,885
  • 1
  • 15
  • 20
  • 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 '16 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)
})
AymKdn
  • 1,189
  • 8
  • 20