List Empty Templates

List Empty Templates

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • Sitecore.Data.Items.TemplateFieldItem
  • Sitecore.Data.Items.TemplateItem
  • Sitecore.ItemIDs
  • Sitecore.TemplateIDs
  • System.Linq
  • System.String

List Empty Templates C# Web Control Code

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

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item templateRoot = db.GetItem(Sitecore.ItemIDs.TemplateRoot);
if (templateRoot == null) return;
string templateRootPath = templateRoot.Paths.Path;

string query = System.String.Format(templateRootPath + "//*[@@templateid='{0}']", Sitecore.TemplateIDs.Template);
Sitecore.Data.Items.Item[] templates = db.SelectItems(query);
if (templates == null) return;

var sortedList = (from entry in templates orderby entry.Paths.Path 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);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "First Ten Templates that do not contain their own fields");
output.RenderEndTag(); //P

int i = 0;
foreach (Sitecore.Data.Items.Item template in sortedList)
{
    Sitecore.Data.Items.TemplateItem templateItem = new Sitecore.Data.Items.TemplateItem(template);
    Sitecore.Data.Items.TemplateFieldItem[] ownFields = templateItem.OwnFields;

    if (ownFields == null | ownFields.Length == 0)
    {
        i++;
        output.Write("{0}", template.Paths.Path + "<br/>");
        //only display first ten items
        if (i >= 10) break;
    }
}
if (i == 0)
    output.Write("{0}", "There are no templates that are empty");

output.RenderEndTag(); //Div


List Empty Templates C# Web Control Output

First Ten Templates that do not contain their own fields

/sitecore/templates/Common/Folder
/sitecore/templates/Redirect Module/Redirect Folder
/sitecore/templates/System/Analytics/Campaign Category
/sitecore/templates/System/Analytics/Goal
/sitecore/templates/System/Analytics/Goal Category
/sitecore/templates/System/Analytics/Page Event Category
/sitecore/templates/System/Analytics/Testing/Multivariate Test Variable
/sitecore/templates/System/Analytics/Testing/Page Level Test Definition
/sitecore/templates/System/Analytics/Testing/Page Level Test Variable
/sitecore/templates/System/ASR/Filter