2

I am using Sitecore 10.1 and I am upgrading my code from the Sitecore 8.2 solutions.

I want to know the best practice to check if the Item is being inherited from a template? The requirement is to use a method (maybe OOTB) that should not reduce the performance. Please suggest.

2 Answers 2

4

There is an OOTB call that handles this now.

protected virtual bool InheritsTemplate(Item candidate, string templateIdentifier)
{
    if (candidate == null || string.IsNullOrEmpty(templateIdentifier))
        return false;

    var t = TemplateManager.GetTemplate(candidate);
    var y = TemplateManager.GetTemplate(templateIdentifier, candidate.Database);

    if (y == null)
        throw new Exception($"Invalid Template Identifier: \'{templateIdentifier}\'");

    return t.InheritsFrom(y.ID);
}

The important parts of this code being:

// Get the template of the Item you want to check
var t = TemplateManager.GetTemplate(candidate);

// Get the template you want to check for
var y = TemplateManager.GetTemplate(templateIdentifier, candidate.Database);

// then use the OOTB call
t.InheritsFrom(y.ID)

templateIdentifier could be anything you can normally send to Sitecore; e.g. an ID or a template path user defined/common folder or whatever.

-1

I just found a blog which might be helpful. Please check this https://kylekingsbury.com/a-faster-isderived-extension-method/

Here is quick code and info I captured from this blog. We can find the updates to the IsDerived extension methods that reduced the time taken.

public static class ItemExtensions
{
    /// <summary>
    /// Determine if the Item inherits a specific template, by ID
    /// </summary>
    /// <param name="item">The item</param>
    /// <param name="templateId">The template id</param>
    /// <returns><c>True</c> if the item inherits from <paramref name="templateId"/></returns>
    public static bool IsDerived(this Item item, ID templateId)
    {
        if (item == null)
        {
            return false;
        }

        return !templateId.IsNull && item.IsDerived(item.Database.GetItem(templateId, item.Language));
    }

    /// <summary>
    /// Determine if the Item inherits a specific template, by TemplateItem
    /// </summary>
    /// <param name="item">The item</param>
    /// <param name="inheritedTemplateItem">the templateItem</param>
    /// <returns><c>True</c> if the item inherits from <paramref name="inheritedTemplateItem"/></returns>
    public static bool IsDerived(this Item item, Item inheritedTemplateItem)
    {
        if (item == null
            || inheritedTemplateItem == null)
        {
            return false;
        }

        var itemTemplate = TemplateManager.GetTemplate(item);

        return itemTemplate != null && (itemTemplate.ID == inheritedTemplateItem.ID || itemTemplate.InheritsFrom(inheritedTemplateItem.ID, item.Database));
    }
}


    public static class TemplateExtensions
{
    public static ID StandardTemplateId = ID.Parse("{1930BBEB-7805-471A-A3BE-4858AC7CF696}");

    /// <summary>
    /// 
    /// </summary>
    /// <param name="template"></param>
    /// <param name="inheritedId"></param>
    /// <param name="database"></param>
    /// <returns></returns>
    public static bool InheritsFrom(this Template template, ID inheritedId, Database database)
    {
        if (template == null || database == null)
        {
            return false;
        }

        if (template.ID == inheritedId)
        {
            return true;
        }

        var hasBaseTemplate = template.BaseIDs.Any(baseId => baseId == inheritedId);

        if (hasBaseTemplate)
        {
            return true;
        }

        var baseTemplates = template.BaseIDs
                                    .Where(baseId => baseId != TemplateExtensions.StandardTemplateId)
                                    .Select(baseId => TemplateManager.GetTemplate(baseId, database));

        return baseTemplates.Any(baseTemplate => baseTemplate != null && baseTemplate.InheritsFrom(inheritedId, database));
    }
}
1
  • This code is obsolete. TemplateManager has code baked in nowadays that does this.
    – Mark Cassidy
    Commented Jan 5, 2022 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.