Tuesday, October 2, 2012

Get all subsites under SharePoint site using ECMA script

In this post, I will explain how to get all subsites under SharePoint site using ECMA script

<script type="text/ecmascript">
var currentcontext = null;
var currentweb = null;
ExecuteOrDelayUntilScriptLoaded(GetSites, "sp.js");
function GetSites()
{
currentcontext = new SP.ClientContext.get_current();
if(currentcontext!=null && currentcontext!=undefined ){
currentweb = currentcontext.get_web();
this.subsites = currentweb.get_webs();
currentcontext.load(this.subsites);
currentcontext.executeQueryAsync(Function.createDelegate(this, this.OnSuccess),
Function.createDelegate(this, this.OnFailure));
}
}
function OnSuccess(sender, args) {
var subsite = '';
var Listsubsite= this.subsites.getEnumerator();
while (Listsubsite.moveNext())
{
var Site = Listsubsite.get_current();
subsite += '\nID: ' + Site.get_id() + '\nTitle: ' + Site.get_title()+'\n Created date:'+ Site.get_created();
}
alert(subsite);
}
function OnFailure(sender, args) {
alert('Error');
}
</script>

No comments:

Post a Comment