I want to count the list items using javascript
2 Answers
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/
-
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.– AlexDec 29, 2016 at 12:16
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)
})