Get Start Item

Get Start Item

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

rootPath Attribute

  • The path to the root item of the site
  • The item path specified in the URL will be appended to this value to locate the item

startItem Attribute

  • The path to the start item of the site
  • Used as the default item path if no path is specified in the URL
  • Combined with rootPath to find the default item of the site

Configuration Settings

<configuration>
   <sitecore>
      <sites>
         <site name="website" rootpath="/sitecore/content" startitem="/home" />
      </sites>

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;
string startItem = site.StartItem;
string startPath = site.StartPath;

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item start = db.GetItem(startPath);
if (start == 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 &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Parent Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Start Item", start.DisplayName);
output.RenderEndTag(); //Div


C# Web Control Real-time Output

Parent Item: Get Start Item

Start Item: Home