Referencing SharePoint SPContext
SharePoint is a highly hierarchal system with site collections, websites and subsites. As a result, one of the tasks you need to do while developing for the SharePoint is to reference site collections and a specific site as well as subsite.
In order to figure out your exact location and get reference to the site, you need to utilize SPContext object. This object has property called Current that allows your code to dynamically decide on the location of your code execution.
Here is an example of SPContext usage: SPContext.Current.Web.Title
The SPContext example above is able to retrieve title of the site code is currently executing under.
Note: you should never Dispose() of objects which are retrieved directly from the SPContext.Current. The SPContext object is continually used and disposing of it will introduce unexpected errors. On the other hand, when you user new SPSite site object from the beginning, you need to Dispose() of it as you always do.
using System; using Microsoft.SharePoint; namespace contentTypeTest { class SharePointApp { static void Main(string[] args) { SPSite site = SPContext.Current.Site; SPWeb web = site.RootWeb; SPWebCollection webs = web.Webs; SPWeb subsiteA = webs["Subsite A"]; } } }