We have created web application.

Within this we created 2 site collection: Site collection 1 - http://pcname:3333/

Site collection 2 - http://pcname:3333/sites/sc2

Within each site collection we created 3 webs.

  1. How to get the root site colelction URL (not root web of sitecollection) via SSOM

  2. How to get the root site colelction URL via ECMA (say on masterpage)

2 Answers 2


Using C# (server-side):

SPSite siteCollection = SPContext.Current.Web.Site;
var rootSiteCollection = siteCollection.WebApplication.Sites[0].Url;

If you have alternate URLs, then use 'AlternateUrls'

On the client side, you could use plain old javascript

var winLoc = window.location;
var siteUrl = winLoc.protocol + '//' + winLoc.host + (winLoc.port ? ':' + winLoc.port : '');
window.console && console.log(siteUrl);

In addition, for client javascript, _spPageContextInfo is your friend.


Your Answer

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

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