Get Context Device

Get Context Device

Context Device

  • The device associated with the current HTTP request

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.DeviceItem
  • Sitecore.Data.Items.Item

Context Device C# Web Control Code

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

Sitecore.Data.Items.DeviceItem device = Sitecore.Context.Device;
if (device == 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

//using : for colon and ? for question mark and   for space
output.Write("{0}&#58;&#160;{1}", "Name", "<b>" + device.Name + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Display Name", "<b>" + device.DisplayName + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Definition Name", "<b>" + device.InnerItem.InnerData.Definition.Name + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Database Name", "<b>" + device.Database.Name + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "Is default device", "<b>" + device.IsDefault.ToString() + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "Is valid device", "<b>" + device.IsValid.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Query String", "<b>" + device.QueryString + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Icon", "<b>" + device.Icon + "</b>");

output.RenderEndTag(); //Div


Context Device C# Web Control Output

Name: Default
Display Name: Default
Definition Name: Default
Database Name: web
Is default device? True
Is valid device? False
Query String: 
Icon: People/16x16/monitor.png