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

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?



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).

  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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