Context Object

Context Object

The Context Object

  • Contains the current HTTP request and the information associated with it
  • Associated information is contained in properties
  • The httpRequestBegin pipeline is used to define the Sitecore context for each HTTP request

Context Object Properties

  • Context Device is the device associated with the current HTTP request
  • Context Site is the Sitecore site activated by the current HTTP request
  • Context Database is the database associated with the current HTTP request
  • Context Language is the language associated with the current HTTP request
  • Context Item is the Sitecore item activated by the current HTTP request
  • Context Domain is the Sitecore security domain associated with the context site
  • Context User is the security user associated with the current HTTP request
  • Context Raw URL provides access to the path and query string components of the current HTTP request, but not the protocol and hostname

Context Object C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == 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

string deviceName = string.Empty, siteName  = string.Empty, dbName = string.Empty;
string languageName = string.Empty, itemName = string.Empty, domainName = string.Empty;
string userName = string.Empty, rawUrl = string.Empty;

Sitecore.Data.Items.DeviceItem contextDevice = Sitecore.Context.Device;
Sitecore.Sites.SiteContext contextSite = Sitecore.Context.Site;
Sitecore.Data.Database contextDB = Sitecore.Context.Database;
Sitecore.Globalization.Language contextLanguage = Sitecore.Context.Language;
Sitecore.Data.Items.Item contextItem = Sitecore.Context.Item;
Sitecore.Security.Domains.Domain contextDomain = Sitecore.Context.Domain;
Sitecore.Security.Accounts.User contextUser = Sitecore.Context.User;
if(contextDevice != null)
    deviceName = contextDevice.Name;
if(contextSite != null)
    siteName = contextSite.Name;     
if(contextDB != null)
    dbName = contextDB.Name;
if(contextLanguage != null)
    languageName = contextLanguage.Name;
if (contextItem != null)
    itemName = contextItem.DisplayName;
if (contextDomain != null)
    domainName = contextDomain.Name;
if (contextUser != null)
    userName = contextUser.Name;
rawUrl = Sitecore.Context.RawUrl;

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ul);
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}: {1}", "Context Device", deviceName);
output.RenderEndTag();

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}: {1}", "Context Site", siteName);
output.RenderEndTag();

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}: {1}", "Context Database", dbName);
output.RenderEndTag();

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}: {1}", "Context Language", languageName);
output.RenderEndTag();

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}: {1}", "Context Item", itemName);
output.RenderEndTag();

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}: {1}", "Context Domain", domainName);
output.RenderEndTag();

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}: {1}", "Context User", userName);
output.RenderEndTag();

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}: {1}", "Context Raw URL", rawUrl);
output.RenderEndTag();

output.RenderEndTag(); //Ul
output.RenderEndTag(); //Div


Context Object C# Web Control Output

  • Context Device: Default
  • Context Site: website
  • Context Database: web
  • Context Language: en
  • Context Item: Context Object
  • Context Domain: extranet
  • Context User: extranet\Anonymous
  • Context Raw URL: /en/Developers/ASPNET/SitecoreAPI/ContextObject

Sources