1

Is it possible to get all Calendar lists from entire SharePoint site collection using JSOM?

I want to get lists from sub sites also.

Thanks.

2 Answers 2

2

The CSOM does not provide a mechanism for querying data across multiple lists within a site collection, in other words, there is no client-side functional equivalent of the SPSiteDataQuery class. Despite that fact, it could be accomplished but the performance could be a real bottleneck here.

The following example demonstrates how to retrieve contact lists within a site collecion:

function enumWebs(rootWeb,propertiesToRetrieve, success,error)
{
   var result = [];
   var level = 0;
   ctx.load(rootWeb,propertiesToRetrieve);
   result.push(rootWeb);
   var colPropertiesToRetrieve = String.format('Include({0})',propertiesToRetrieve.join(',')); 
   var enumWebsInner = function(web,result,success,error) 
   {
      level++;
      var ctx = web.get_context();
      var webs = web.get_webs(); 
      ctx.load(webs,colPropertiesToRetrieve);
      ctx.executeQueryAsync(
        function(){
            for(var i = 0; i < webs.get_count();i++){
                var web = webs.getItemAtIndex(i);
                result.push(web);
                enumWebsInner(web,result,success,error);
            }
            level--;
            if (level == 0 && success)
              success(result);  
        },
        error);
   };
   enumWebsInner(rootWeb,result,success,error);    
}


var ctx = SP.ClientContext.get_current();
var rootWeb = ctx.get_site().get_rootWeb();
enumWebs(rootWeb,['Lists'],
   function(allWebs){
       var calendarLists = allWebs.map(function(web){
            var lists = web.get_lists().get_data().filter(function(list){
                 return list.get_baseTemplate() === 106;
            }); 
            return lists; 
       }); 

       //print results
       console.log(calendarLists.length + ' calendars were found in site collection');

   },
   function(sender,args){
      console.log(args.get_message());
   });  
1

You can not get all calendar lists under a site collection at a time. You will have to query site by site. For example, If you make a GET request to below endpoint, it will return all calendar lists.

/_api/web/lists?$filter=BaseTemplate eq 106

So you have to make this GET request for all sites and sub sites.

PS: Template Id of calendar is 106. That's why you need to apply $filter equal 106

Use my SharePoint REST Client to explore more.

Your Answer

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

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