Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

With SPContext.Current I can get the current site or current web. However, I would like to get the root site collection for my current web application. I know that I can get the web application from SPContext.Current.Site.WebApplication but is there a simple way to get the root site collection from that?

share|improve this question

4 Answers 4

up vote 10 down vote accepted
// root site – eg. Web App url
string rootSiteCollectionURL = SPContext.Current.Web.Site.WebApplication.Sites[0].Url;

// site collection url
string SiteCollectionURL = SPContext.Current.Web.Site.Url;
share|improve this answer
1  
is this guaranteed to contain the root site collection though? –  Nacht Oct 23 '12 at 23:32

here's a post that may be of help - Programmatically Access Root Site Collection in Multi-Site Collection Heirarchy.

share|improve this answer
    
string rootSiteCollectionURL = SPContext.Current.Web.Site.WebApplication.Sites[0].Url; // from the link above –  Supriyo SB Chatterjee Jun 27 '12 at 16:17

using (var site = new SPSite("url"))

{ using (SPWeb web = site.RootWeb)

{ string url = web.Site.RootWeb.Url;

} }

share|improve this answer

Try : SPSite.RootWeb. More information at http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.rootweb.aspx

share|improve this answer
    
He does not want the RootWeb, but the "Root Site" –  Robert Lindgren Jun 25 at 11:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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