SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using JS to get the number of (filtered) listitems to show on a dashboard. My functions work well when I call them once but I need to call them multiple times. Only the last call is processed. What am I doing wrong here?

ExecuteOrDelayUntilScriptLoaded(SetNumbers, "sp.js");
var clientContext = null;
var web = null;

function SetNumbers(){

function CountProjecten(listName, divId, opleidingId)

clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();        
var list = web.get_lists().getByTitle(listName);        
var camlQuery = new SP.CamlQuery();        
var q = "<View><Query><Where><And><Eq><FieldRef Name='Status' LookupId='True' /><Value Type='Lookup'>3</Value></Eq><Eq><FieldRef Name='Opleiding' LookupId='True' /><Value Type='LookupMulti'>" + opleidingId + "</Value></Eq></And></Where></Query></View>";
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;

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
share|improve this question
Are you calling it multiple times quickly in succession? Sorry your code is a little hard to read, I'm going to quickly format it for you. Then I will have a read. – Hugh Wood Nov 13 '12 at 10:12
up vote 0 down vote accepted

All your requests are processed correctly, but the callback onListItemsLoadSuccess is overwritten, especially this.listItems and this.divId and runs only with the last result. So the problem is this line:


this.divId is everytime overwritten and is the last divId which is "#pbaktrm" in your case. this is appended to the Window object in this case and makes your variables global and they are overwritten every time you change them.

So to quickly solve your problem you can keep divId and listItems inside the function CountProjecten and use an anonymous function as success callback:

function CountProjecten(listName, divId, opleidingId) {

    clientContext = SP.ClientContext.get_current();
    web = clientContext.get_web();
    var list = web.get_lists().getByTitle(listName);
    var camlQuery = new SP.CamlQuery();
    var q = "OMMITTED FOR BREVITY"; 
    var listItems = list.getItems(camlQuery);
    clientContext.load(listItems, 'Include(Id)');
    clientContext.executeQueryAsync(function () {
        var count = 0;
        count = listItems.get_count();
    }, Function.createDelegate(this, this.onQueryFailed));


But in your code there is another improvement that can be made: You don't have to call the server 13 (!) times. It can slow down your users' web browsers. What you can do is to load all your items and then run clientContext.executeQueryAsync.

share|improve this answer

From what I see, each call is overwriting the previous one because inside CountProjecten you are defining clientContext and web as global variables. Instead, try:

var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web(); 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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