0

Here is my code:

var count = 0;

function retrieveCurrentListProperties() {
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();
    var list = web.get_lists().getByTitle("Urgent Alerts");
    var camlQuery = new SP.CamlQuery();
    var q =  "<View><Query><Where><Eq><FieldRef Name='End_x0020_Date'/><Value Type='DateTime'><Today/></Value></Eq></Where></Query></View>";
    camlQuery.set_viewXml(q);
    this.listItems = list.getItems(camlQuery);
    clientContext.load(this.listItems);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onCListItemsLoadSuccess), 
    Function.createDelegate(this, this.onQueryFailed));

}

function onCListItemsLoadSuccess(sender, args) {

    var count1 = 0;
    var listEnumerator = this.listItems.getEnumerator();
    //iterate though all of the items

count1 = this.listItems.get_count();


}




function onQueryFailed(sender, args) {
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}



                count = 1;

This code retrieves a list from sharepoint and then counts how many items are in that list. I need count1 to be used in another part where count=1 but obviously if I did count=count1 it would throw an error. How can I used count1 in the way that I want

4
  • "but obviously if I did count=count1 it would throw an error" - why? Commented Mar 31, 2014 at 16:14
  • count and count1 are both this.listItems.get_count(). Declare it globally and initialize properly. Commented Mar 31, 2014 at 16:14
  • Using variables outside the methods they are assigned to in is easy. Using variables before they have been assigned to is tricky. Commented Mar 31, 2014 at 16:15
  • where are you doing count = count1? It is just because of the scoping Commented Mar 31, 2014 at 16:16

1 Answer 1

1

Since SP.ClientContext.executeQueryAsync method executes the current pending request asynchronously on the server, two approaches are commonly used to control the sequential execution of asynchronous calls in SharePoint.

  • Callbacks
  • Deferred

With callback approach you declare your function like this

function getData(Success,Error) {
   //...
   clientContext.executeQueryAsync(function() {
             var result = ...
             Success(result);
           },
           Error
    );
} 

Deferred approach is based on a Promises pattern, please refer this article for a details about the usage of Promises with CSOM.

Example with callback approach

function getItemsCount(listTitle,Success,Error) {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var list = web.get_lists().getByTitle(listTitle);
    var qry = SP.CamlQuery.createAllItemsQuery();
    var listItems = list.getItems(qry);
    clientContext.load(listItems);
    clientContext.executeQueryAsync(function() {
         var count = listItems.get_count();
         Success(count);
       },
       Error
    );
}


//Usage
getItemsCount('Tasks', function(tasksCount){
    //...
    console.log('Tasks count:' + tasksCount);
   },
   function(sender, args) {
    console.log('Error:' + args.get_message());
   }
);

Recommendations

Avoid global variables

Wrap your code in a scoping function and use variables local to that scoping function, and make your other functions closures within it

(function() { // Begin scoping function
    var clientContext; // Global to your code, invisible outside the scoping function
    function loadListItems(listTitle) {
        // ...
    }
})();    

Usage of Function.createDelegate

In most cases, there is no need to wrap handlers using Function.createDelegate, instead you could write:

ctx.executeQueryAsync(succeeded,failed);

or

ctx.executeQueryAsync(function(){
    //...
  },
  function(sender,args){
    //Error handling goes here
  }
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.