Accessing the Top-Level Site within a Site Collection from anywhere.

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



About Geoff Varosky
Geoff Varosky is a Senior Architect for Insight, based out of Watertown, MA. He has been architecting and developing web based applications his entire career, and has been working with SharePoint for the past 15 years. Geoff is an active member of the SharePoint community, Co-Founder and Co-Organizer of the Boston Area SharePoint Users Group, co-founder for the Boston Office 365 Users Group, co-organizer for SharePoint Saturday Boston and speaks regularly at SharePoint events and user groups.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: