Context Database

Context Database

Sitecore.Data.Database

  • Used to represent a Sitecore database
  • Used to access items using absolute queries; use axes methods for relative queries
  • Sitecore.Context.Database derives from this class
  • Properties and methods provide access to items in database

Context Database

  • The Sitecore database associated with the current HTTP request
  • Presentation components that retrieve content for display almost always access the context database rather than referencing a database by name

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item

C# Web Control Code

Sitecore.Data.Database db = Sitecore.Context.Database; 
Sitecore.Data.Items.Item item = Sitecore.Context.Item; 
if (db == null || 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: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}", "Archives Count", "<b>" + db.Archives.Count.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Branches Base Path", "<b>" + db.Branches.BasePath + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Data Cache ItemCount", "<b>" + db.Caches.DataCache.InnerCache.Count.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Data Cache Name", "<b>" + db.Caches.DataCache.Name + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Item Cache Enabled", "<b>" + db.Caches.ItemCache.Enabled.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Path Cache Name", "<b>" + db.Caches.PathCache.Name + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Standard Values Cache Name", "<b>" + db.Caches.StandardValuesCache.Name + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Has Content Item", "<b>" + db.HasContentItem.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Icon", "<b>" + db.Icon + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Name", "<b>" + db.Name + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Version", "<b>" + db.Properties.Version.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Protected", "<b>" + db.Protected.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Proxies Enabled", "<b>" + db.ProxiesEnabled.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Publish Virtual Items", "<b>" + db.PublishVirtualItems.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Read Only", "<b>" + db.ReadOnly.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Security Enabled", "<b>" + db.SecurityEnabled.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Sitecore Item Axes Root", "<b>" + db.SitecoreItem.Axes.Root.ToString() + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Workflow Provider", "<b>" + db.WorkflowProvider.ToString() + "</b>"); 
output.RenderEndTag(); //Div