1
votes

I have some templates which define the same fields like 'Page Title' or 'Page Name' or something, but they're defined in different sections.

When a content item uses both of these templates, of course they both show up in their sections when editing the item. What is the best way to find these occurances?

I was writing some code to inspect all the templates, recursively look at template inheritance, and find duplicates, but that was getting out of hand.

What about looking at all the content items themselves to see all fields from all sections, and see if there's overlap? This would get me where I need to go, but I don't see how to do that given a Sitecore Item.

1

1 Answers

2
votes

Code below loops through all templates and check for duplicated field keys:

TemplateDictionary dict = TemplateManager.GetTemplates(Database.GetDatabase("master"));

List<string> duplicates = new List<string>();

string reportLine = "template '{0}': field '{1}' duplicated from templates '{2}'";

foreach (KeyValuePair<ID, Template> templatePair in dict)
{
    foreach (IGrouping<string, TemplateField> grouping in templatePair.Value.GetFields(true).GroupBy(f => f.Key).Where(f => f.Count() > 1))
    {
        duplicates.Add(String.Format(reportLine, templatePair.Value.FullName, grouping.Key, String.Join(", ", grouping.Select(g => g.Template.FullName))));
    }
}

It will return you a list of duplicated fields in a format

template 'System/Analytics/Engagement Automation/Engagement Plan State': field 'description' duplicated from templates 'System/Workflow/State, System/Analytics/Engagement Automation/Designer/Designer Item Parameters'