Accessing the Top-Level Site within a Site Collection from anywhere.
December 18, 2008 Leave a comment
If you need to programmatically access the top-level site within a site collection, and do not want to have to statically set any URLs, GUIDs, or anything like that when trying to open a web, you can access it as site.AllWebs[0] (where site is the site collection as shown below), as the first site in a site collection will always be the top level site.
using (SPSite site = new SPSite(SPContext.Current.Site.ID)) { using (SPWeb web = site.AllWebs[0]) { // Code here } }