Sitecore: base template item of a page

641 Views Asked by At

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:

  • Page called A1 of template A
  • Template A has as base template the template B
  • B is part of page A1 as an instance called B1 (field values specific for the page A1)

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

2

There are 2 best solutions below

1
nologo On BEST ANSWER

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);
        }
0
grandeale83 On

I was getting confused about the contents of the''yml. There is no instance of the base template within the page item