asp.netasp.net-mvcsitecoresitecore8.1

Sitecore: base template item of a page


I have a page Item in Sitecore, and I know it is derived from a certain base template. I have that base template ID, but I want to get the instance of this base template that is part of my page. I want it as an item, so for example I can get the ID of the template internal of the page.

To be more clear:

How can I get B1 as stand alone Item/ID?
Any help is appreciated.
Thanks.


Solution

  • Not really sure what you mean? why would you want to get the instance of an inheriting template? the item would have all fields you've inherited available anyway via item.fields[templateBFieldId].Value

    You could do something like check if the current item inherits from that template.. so something like:

    public static bool InheritsFrom(this Item item, ID templateId)
        {
            return item.Template.DoesTemplateInheritFrom(templateId);
        }
    
        public static bool DoesTemplateInheritFrom(this TemplateItem template, ID templateId)
        {
            if (template == null || templateId.IsNull)
                return false;
            if (!(template.ID == templateId))
                return template.DoesTemplateInheritFrom(TemplateManager.GetTemplate(templateId, template.Database));
            return true;
        }
    
    private static bool DoesTemplateInheritFrom(this TemplateItem template, Template baseTemplate)
            {
                if (baseTemplate == null)
                    return false;
    
                return TemplateManager.GetTemplate(template.ID, template.Database).DescendsFromOrEquals(baseTemplate.ID);
            }