0

I want executeQueryAsync to complete before giving back control to webEnumerator.moveNext, anyway ?

function onGetSubwebsSuccess(sender, args) {
    var webEnumerator = webCollection.getEnumerator();

    while (webEnumerator.moveNext()) {
        alert("Loop 1");
        web = webEnumerator.get_current();
        this.props = web.get_allProperties();
        context.load(this.props);
        context.executeQueryAsync(Function.createDelegate(this, gotProperty), Function.createDelegate(this, failedGettingProperty));
    }
}

function gotProperty() {
    var myPropBag = this.props;
    alert("Loop 2");
}

What's happening now is, I get alert for "Loop 1" twice and then alert for "Loop 2" when it should be like this,

Loop 1
Loop 2
Loop 1
Loop 2

2 Answers 2

0

Since Web properties (Web.AllProperties) could be requested using a single query using the following syntax:

clientContext.load(webs,'Include(Url,AllProperties)');

you don't need to perform any subsequent asynchronous requests using executeQueryAsync method to load web properties per every web.

The following example demonstrates how to load web properties from sub webs and save them into websProps array:

(function(){
  var ctx = SP.ClientContext.get_current();
  var webs = ctx.get_web().get_webs();
  var websProps = {};
  ctx.load(webs,'Include(Url,AllProperties)');  //Note: Explicitly specify which properties to load for Web  
  ctx.executeQueryAsync(function(){
       var e = webs.getEnumerator();
       while (e.moveNext()) {
          var web = e.get_current();
          var webUrl = web.get_url();
          var props = web.get_allProperties();
          var propNameValues = props.get_fieldValues();
          //save properties  
          //Note: For demonstration purposes vti_defaultlanguage property is saved, change it to your property name
          websProps[webUrl] = {'DefaultLanguage': propNameValues['vti_defaultlanguage']};
       }
       console.log(JSON.stringify(websProps)); 
    },
    function(sender, args){
        //Handle error
    }
  );
})();
0
1

This happens because of the asynchronous behaviour of client object model. If you want to wait till the code in executequeryasync completes then you can use Jquery deferreds. Here is a nice article explaining how to use deferreds http://scenicsharepoint.blogspot.in/2012/12/using-jquery-deferred-object-with.html?m=1

4
  • Uncaught TypeError: Object function (a,b){return new c.fn.init(a,b)} has no method 'Deferred' Commented Apr 12, 2014 at 10:50
  • how can i apply in the example here please help i donot know how to make this work Commented Apr 12, 2014 at 10:51
  • Sorry, I am on mobile and can't do much. However, make sure that you have added reference to JQUERY. Deferreds are part of JQUERY Commented Apr 12, 2014 at 11:21
  • yes i have the jquery reference but its not working Commented Apr 12, 2014 at 14:43

Your Answer

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

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