Skip to main content
deleted 204 characters in body
Source Link
                var promises=[];
                var subSiteEnum = SubSites.getEnumerator();
            
                while (subSiteEnum.moveNext()) {
                
                var res = (function getPropertyBags() {
                        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.rejectfail(args);
                        });
                     );
   
                   
       return dfd.promise(); 
            return dfd.promise();  
       
           
           
       
               }//getPropertyBags)();   
                    
                    
                    res().done( function( data ) {
                         var Prop = data.get_fieldValues()["projectManager"];["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');
                }
                var promises=[];
                var subSiteEnum = SubSites.getEnumerator();
            
                while (subSiteEnum.moveNext()) {
                
                var res = function getPropertyBags() {
                        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.reject(args);
                        });
                        
                         return dfd.promise(); 
                     
                     
                     }//getPropertyBags   
                    
                    
                    res().done( function( data ) {
                         var Prop = data.get_fieldValues()["projectManager"];
                         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');
                }
                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');
                }
Source Link

Loop though subsites and get all Property Bags - jQuery deferred issue

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 getPropertyBags() {
                        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.reject(args);
                        });
                        
                        return dfd.promise(); 
                    
                    
                    }//getPropertyBags   
                    
                    
                    res().done( function( data ) {
                         var Prop = data.get_fieldValues()["projectManager"];
                         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');
                }