Get Database

Get Database

Controls featured in this example:

    Sitecore Web Control

Classes featured in this example:

    Sitecore.Configuration.Factory
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • System.Text.StringBuilder

Database C# Web Control Code

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

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master");
System.Text.StringBuilder builder = new System.Text.StringBuilder();

builder.Append("Database Name: <b>" + db.Name + "</b><br/>");
builder.Append("Connection String Name: <b>" + db.ConnectionStringName + "</b><br/>");
builder.Append("Has Content Item? <b>" + db.HasContentItem.ToString() + "</b><br/>");
builder.Append("Icon: <b>" + db.Icon + "</b><br/>");

Sitecore.Globalization.Language[] languages = db.Languages;
foreach (var language in languages)
{
    builder.Append("Culture Name: <b>" + language.CultureInfo.DisplayName + "</b><br/>");
    builder.Append("Native Name: <b>" + language.CultureInfo.NativeName + "</b><br/>");
    builder.Append("English Name: <b>" + language.CultureInfo.EnglishName + "</b><br/>");
}

Sitecore.Data.Clones.NotificationProvider notifyProvider = db.NotificationProvider;
System.Collections.Generic.IEnumerable<Sitecore.Data.Clones.Notification> notifications = notifyProvider.GetNotifications();
foreach(Sitecore.Data.Clones.Notification notify in notifications)
    builder.Append("Notification URI: <b>" + notify.Uri + "</b><br/>");

Sitecore.Globalization.Language lang = Sitecore.Context.Language;
string targetDB = "web";
Sitecore.Data.DatabaseProperties properties = db.Properties;
builder.Append("Database Version: <b>" + properties.Version.ToString() + "</b><br/>");
builder.Append("Smart Publish Required? <b>" + properties.GetSmartPublishRequired(targetDB, lang).ToString() + "</b><br/>");

builder.Append("Database Protected? <b>" + db.Protected.ToString() + "</b><br/>");
builder.Append("Proxies Enabled? <b>" + db.ProxiesEnabled.ToString() + "</b><br/>");
builder.Append("Publish Virtual Items? <b>" + db.PublishVirtualItems.ToString() + "</b><br/>");
builder.Append("Read Only? <b>" + db.ReadOnly.ToString() + "</b><br/>");
builder.Append("Publish Virtual Items? <b>" + db.PublishVirtualItems.ToString() + "</b><br/>");
builder.Append("Security Enabled? <b>" + db.SecurityEnabled.ToString() + "</b><br/>");

Sitecore.Resources.ResourceItems resources = db.Resources;
Sitecore.Data.LayoutRecords deviceLayouts = resources.Layouts;
builder.Append("Layouts Root: <b>" + deviceLayouts.BasePath + "</b><br/>");

Sitecore.Data.RenderingRecords deviceRenderings = resources.Renderings;
builder.Append("Renderings Root: <b>" + deviceRenderings.BasePath + "</b><br/>");

Sitecore.Data.SublayoutRecords deviceSublayouts = resources.Sublayouts;
builder.Append("Sublayouts Root: <b>" + deviceSublayouts.BasePath + "</b><br/>");

Sitecore.Data.Items.Item sitecore = db.SitecoreItem;
builder.Append("Sitecore Display Name: <b>" + sitecore.DisplayName + "</b><br/>");
builder.Append("Sitecore Path: <b>" + sitecore.Paths.Path + "</b><br/>");
builder.Append("Sitecore Content Path: <b>" + sitecore.Paths.ContentPath + "</b><br/>");
builder.Append("Sitecore Full Path: <b>" + sitecore.Paths.FullPath + "</b><br/>");
        
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("{0}", "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);
output.Write("{0}&#58;&#160;{1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}", builder.ToString());

output.RenderEndTag(); //Div


Database C# Web Control Output

Database Name: master
Connection String Name: master
Has Content Item? True
Icon: People/16x16/cubes_blue.png
Culture Name: English
Native Name: English
English Name: English
Database Version: 500
Smart Publish Required? True
Database Protected? False
Proxies Enabled? False
Publish Virtual Items? True
Read Only? False
Publish Virtual Items? True
Security Enabled? True
Layouts Root: /sitecore/layout/layouts
Renderings Root: /sitecore/layout/renderings
Sublayouts Root: 
Sitecore Display Name: sitecore
Sitecore Path: /sitecore
Sitecore Content Path: /sitecore
Sitecore Full Path: /sitecore

Notes

  • Sitecore.Configuration.Factory.GetDatabase() is used to connect to a specific named database