I have a SharePoint 2010 web site with a master page that has a counter on it. I'm trying to update that counter using the ECMAScript. The code I have falls nicely into the success method - but I cannot find the right call to get a count. I have tried length, length(), count & count(). Does anybody have any suggestions? I have the code working, but only by enumerating through the results of the list and incrementing a count.
This is what I have so far, as an example:-
<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
var list = web.get_lists().getByTitle("Absences");
var camlQuery = new SP.CamlQuery();
var q = "<Where><Eq><FieldRef Name='StatusID' /><Value Type='Integer'>1</Value></Eq></Where>";
camlQuery.set_viewXml(q);
this.listItems = list.getItems(camlQuery);
clientContext.load(listItems, 'Include(Id)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),
Function.createDelegate(this, this.onQueryFailed));
}
function onListItemsLoadSuccess(sender, args) {
var count = 0;
var listEnumerator = this.listItems.getEnumerator();
//iterate though all of the items
while (listEnumerator.moveNext()) {
count = count + 1;
}
$('#spnNumberOfAbsences').text(count);
}
function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>