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

Is there a way to get the root parent site, no matter how many levels deep you are in sub-sites? For example, in this structure:

- MySiteCollection
  - MyRootSite
    - MySubSite
      - MySubSiteOfSubsite
        -AnotherSubSite

So in the above example, I need to get the title property of the MyRootSite web site (not the MySiteCollection title) no matter which sub-site I'm in. I'm thinking server-side code needs to traverse the parent(s) and get the web site one before IsRootWeb becomes true. Is this correct?

Thanks.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

SPSite.RootWeb is the object you're after, in server side code ( http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.rootweb.aspx).

SP.Site.get_rootWeb() in client object model code ( http://msdn.microsoft.com/en-us/library/ee552834.aspx).

share|improve this answer
    
Thanks, James. I'm using this code but it's getting the site collection root, not MyRootSite from above example: using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb) { Response.Write("root: " + oWebsiteRoot.Title); }. What am I doing wrong? –  Alex C Oct 27 '11 at 6:21
2  
I think you're confusing the definition of Site Collection and Web sites. Site Collections strictly don't contain content, only Web Sites. A Site collection has only one Root Web Site, at the top level, which is found by SPSite.RootWeb, and it's impossible for a user to navigate any higher in a particular site collection. –  James Love Oct 27 '11 at 8:23
    
Thanks, James. That makes sense. This provided a recursive method to find the parent that I'm looking for: lawo.wordpress.com/2011/03/21/… I fetch the title inside the if (before the using). –  Alex C Oct 27 '11 at 8:40
add comment

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.