Get Home Item

Get Home Item

Home Item

  • The item within a logical Web site that represents the home page
  • Use one of the following C# techniques to retrieve the home item:
    • Create an object for the database using the context database. Then, create an object for the target item in the database and provide the relative path to the item in the Content Tree, such as “home/about/item1” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/content/home/about/item1”– a null value will be set in either case if the item does not exist

    string homePath = "/sitecore/content/Home"; Sitecore.Data.Database db = Sitecore.Context.Database; Sitecore.Data.Items.Item item = db.GetItem(homePath);

    • Retrieve the Home item from an entry in the web.config

    string startPath = Sitecore.Context.Site.StartPath.ToString(); Sitecore.Data.Items.Item home = Sitecore.Context.Database.GetItem(startPath);

    • Retrieve the home item from the context database

    string homePath = "/sitecore/content/Home"; Sitecore.Data.Items.Item homeItem = Sitecore.Context.Database.GetItem(homePath);

    • Retrieve the home item from the context database and the context site

    Sitecore.Sites.SiteContext site = Sitecore.Context.Site; Sitecore.Data.Items.Item item = Sitecore.Context.Database.GetItem(site.StartPath);

    • Write a method that uses recursion to identify the home item from the data template it is based on
  • Use one of the following XSLT techniques to retrieve the home item:
    • Use Sitecore XSLT extension sc:item...
      <xsl:variable name="home" select="sc:item('/sitecore/content/home',.)" />
    • Use XPath syntax...
<xsl:variable name="home" select="/*/item[@key='content']/item[@key='home']" />
    • Use ancestor axis recursion

      <xsl:variable name="home" select="$sc_currentitem/ancestor-or-self::item[@template='home page']" />

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

Home Item C# Web Control Code

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

Sitecore.Data.Items.Item home1 = Sitecore.Context.Database.GetItem("/sitecore/content/Home");
if (home1 == null) return;

Sitecore.Data.Items.Item home2 = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath);
if (home2 == null) return;

Sitecore.Data.Items.Item[] ancestors = item.Axes.SelectItems("ancestor::*");
if (ancestors == 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}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}&#58;&#160;{1}", "Home Page Title from GetItem() and Path", "<b>" + home1["title"] + "</b>");
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}&#58;&#160;{1}", "Home Page Title from StartPath in web.config", "<b>" + home2["title"] + "</b>");
output.RenderEndTag(); //P

foreach (Sitecore.Data.Items.Item ancestor in ancestors)
{
    if (ancestor.Name == "Home")
    {
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
        output.Write("{0}&#58;&#160;{1}", "Home Page Title from Sitecore Query and Name", "<b>" + ancestor["title"] + "</b>");
        output.RenderEndTag(); //P
    }

    if (ancestor.TemplateName == "TIC-HomePage")
    {
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
        output.Write("{0}&#58;&#160;{1}", "Home Page Title from Sitecore Query and Template Name", "<b>" + ancestor["title"] + "</b>");
        output.RenderEndTag(); //P
    }
}         

output.RenderEndTag(); //Div


Home Item XSLT Rendering Code

<br/>
<xsl:variable name="home1" select="sc:item('/sitecore/content/home',.)" />
<xsl:variable name="home2" select="sc:item('{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}',.)" />
<xsl:variable name="home3" select="$sc_item/ancestor-or-self::item[@template='tic-homepage']" />

<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>
</div>

<div style="font-size:x-small; width:100%; clear:both">
<p><b>Item: <sc:text field="title" select="$sc_item" disable-web-editing="true"/></b></p>

<p><xsl:value-of select="'Home Page Title from XSLT variable and path: '"/> <b><sc:text field="title" select="$home1"/></b></p>
<p><xsl:value-of select="'Home Page Title from XSLT variable and GUID: '"/> <b><sc:text field="title" select="$home2"/></b></p>
<p><xsl:value-of select="'Home Page Title from XPATH ancestor recursion: '"/> <b><sc:text field="title" select="$home3"/></b></p>

</div>


Home Item C# Web Control Output

Home Page Title from GetItem() and Path: Resources for Sitecore Users

Home Page Title from StartPath in web.config: Resources for Sitecore Users

Home Page Title from Sitecore Query and Name: Resources for Sitecore Users

Home Page Title from Sitecore Query and Template Name: Resources for Sitecore Users



Home Item XSLT Rendering Output

Home Page Title from XSLT variable and path: Resources for Sitecore Users

Home Page Title from XSLT variable and GUID: Resources for Sitecore Users

Home Page Title from XPATH ancestor recursion: Resources for Sitecore Users

Sources