0

I'm trying to loop through all subsites under a site collection, get a Property Bag per site and push all the gathered results into an array. I'm having trouble checking if all the jQuery promises have been resolved so I can process the array. Please let me know if I am missing something:

                var promises=[];
                var subSiteEnum = SubSites.getEnumerator();

                while (subSiteEnum.moveNext()) {

                var res = (function(){
                    var dfd = $.Deferred();
                    var Site = subSiteEnum.get_current();
                    var webTemplate = Site.get_webTemplate();       
                    var SiteContext = new SP.ClientContext(Site.get_url());
                    var allProperties = SiteContext.get_web().get_allProperties();

                    SiteContext.load(allProperties);

                    if(webTemplate != 'APP')
                    SiteContext.executeQueryAsync(
                        function() {
                            dfd.resolve(allProperties);
                        },
                        function(sender, args) {
                            dfd.fail(args);
                        }
                    );

                    return dfd.promise(); 


               })();   


                res.done( function( data ) {
                     var Prop = data.get_fieldValues()["projectSector"];
                     if (Prop != undefined) {
                        console.log('pushing: ' + Prop);
                        projectSubSites.push({
                            projectManager: Prop
                        });
                     }
                });

                res.fail( function( data ) {
                    console.log(data);
                });

                promises.push(res);

                } //end while   

                console.log(promises);
                $.when.apply($, promises).then(allDone);

                function allDone(){
                    console.log(projectSubSites);
                    console.log('processed all items');
                }

1 Answer 1

1

Is the stuff with promises important? If not why not just use 'Include(AllProperties)' to include the properties and do something like this:

var context = SP.ClientContext.get_current();
var webs = context.get_site().get_rootWeb().get_webs();
context.load(webs, 'Include(AllProperties)');
context.executeQueryAsync(function(){
  var enumerator = webs.getEnumerator();
  while (enumerator.moveNext()) {
    console.log(enumerator.get_current().get_allProperties().get_fieldValues());
  }
});
1
  • Thanks! I think I can work with that solution. I can't believe I was trying to make it so complicated. It's hard being an SP noob haha.
    – Nimrod P
    Apr 9, 2014 at 17:59

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.