Using Template IDs

Using Template IDs

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

Template IDs C# Web Control Code

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

//using template GUID for TIC-Information template
string query = "/sitecore/content/home/projectleads//*[@@templateid='{E4D2D8A2-A2A5-465D-B7A5-191559CC6A9F}']";

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item[] descendants = db.SelectItems(query);
if (descendants == null) return;
var sortedList = (from entry in descendants orderby entry.Fields["title"].Value ascending select entry);

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);
string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", displayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Displaying Ten Filtered Descendants");
output.RenderEndTag(); //P

int i = 0;
foreach (Sitecore.Data.Items.Item descendant in sortedList)
{
    i++;
    output.Write("{0}", descendant.DisplayName + "<br/>");
    //only display first ten items
    if (i >= 10) break;
}

output.RenderEndTag(); //Div


Template IDs C# Web Control Output

Displaying Ten Filtered Descendants

Architecture Factors
Architecture Guidelines
Basecamp
Combining Sites
Content Inventory
Content Management
Content Tree
Control Types
Creating Components
Data Infrastructure

Notes

  • This snippet uses a Sitecore query to create the filtered list and Linq to sort the items