Tuesday, October 2, 2012

Get Top site collection using ECMA script

In this post, I will explain how to get top site collection using ECMA script

 <script type="text/ecmascript">
var currentcontext = null;
var currentsitecol = null;
ExecuteOrDelayUntilScriptLoaded(GetTopLevelSiteCollection, "sp.js");
function GetTopLevelSiteCollection()
{
currentcontext = new SP.ClientContext.get_current();
if(currentcontext!=null && currentcontext!=undefined ){
currentsitecol= currentcontext.get_site().get_rootWeb();
currentcontext.load(currentsitecol);
currentcontext.executeQueryAsync(Function.createDelegate(this, this.OnSuccess),
Function.createDelegate(this, this.OnFailure));
}
}
function OnSuccess(sender, args) {
var sitecol='';
sitecol+= '\nID: ' + currentsitecol.get_id() + '\nTitle: ' + currentsitecol.get_title()+'\n Created date:'+ currentsitecol.get_created();
alert(sitecol);
}
function OnFailure(sender, args) {
alert('Error');
}
</script>

No comments:

Post a Comment