Get Context Site

Get Context Site

Context Site

  • Corresponds to the first logical Web site that matches various properties of the current HTTP request
  • Defines the default context domain

Obtaining the Context Site

  • Sitecore.Sites.SiteContext site = Sitecore.Context.Site;
  • Sitecore.Sites.SiteContext site = Sitecore.Sites.SiteContext.Current;

Context Site C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

Sitecore.Sites.SiteContext site = Sitecore.Context.Site;
if (site == null) return;

//Alternate way to obtain context site
Sitecore.Sites.SiteContext altSite = Sitecore.Sites.SiteContext.Current;
if (altSite == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Using Sitecore.Context", "<b>" + site.Name + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Using Sitecore.Sites.SiteContext", "<b>" + altSite.Name + "</b>");
output.RenderEndTag(); //Div


Context Site C# Web Control Output

Using Sitecore.Context: website
Using Sitecore.Sites.SiteContext: website