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.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityThe 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());
});
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.