Getting Items

Getting Items

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

  • Sitecore.Configuration.Settings
  • Sitecore.Context
  • Sitecore.Data.Items.Item

Getting Items C# Web Control Code

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item home1 = db.Items["/sitecore/content/home"];
//using GUID of home item
Sitecore.Data.Items.Item home2 = db.Items[new Sitecore.Data.ID("{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}")];
if (home1 == null || home2 == 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", home1.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Title Using Path", "<b>" + home1.Fields["title"] + "</b><br/>"); 
output.Write("{0}&#58;&#160;{1}", "Title Using GUID", "<b>" + home2.Fields["title"] + "</b>");

output.RenderEndTag(); //Div


Getting Items XSLT Rendering Code

<xsl:variable name="home1" select="sc:item('/sitecore/content/home',.)" />
<xsl:variable name="home2" select="sc:item('{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}',.)" />
<xsl:if test="$home1">
    <br/>
    <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">
        <!-- using &#58 for colon and &#160 for space -->
        <p><b><xsl:value-of select="'Item'" />&#58;&#160;<xsl:value-of select="$home1/@name" /></b></p>
       <p><xsl:value-of select="'Title Using Path'"/>&#58;&#160;<b><sc:text field="title" select="$home1"/></b><br/>
       <xsl:value-of select="'Title Using GUID'"/>&#58;&#160;<b><sc:text field="title" select="$home2"/></b></p>
    </div>
</xsl:if>


Getting Items C# Web Control Output

Title Using Path: Resources for Sitecore Users
Title Using GUID: Resources for Sitecore Users



Getting Items XSLT Rendering Output

Title Using Path: Resources for Sitecore Users
Title Using GUID: Resources for Sitecore Users

Notes

  • Referencing GUIDs provides faster performance than names, and names can change
  • Sitecore provides specialized classes for getting templates and media items