Search code examples
sitecoresitecore8

Retrofitting inheritance into a Sitecore template


Can anyone tell me if its possible to retrofit inheritance into a Sitecore template?

I have a task to add a new page field into multiple existing templates and I think this should be in a base template.

I've also noticed that the existing templates have fields that should be moved to a base template and then inherited from. Is this possible and if so will there be any side effects with existing code / data?


Solution

  • Yes, it's absolutely ok to add extra base templates to existing Sitecore templates.

    E.g. if you already have multiple "page" templates and you need to add extra token for some tracking, you can create new template (let's say "ICustomTracking") and then add it to base templates of your page templates.

    To answer your second question: you can "extract" base templates from the existing templates. If you don't want to loose any data, the order of your actions is:

    1. Create new base template
    2. Set this template as a base template for all templates you want it to inherit from.
    3. Move field item from the inheriting template to the base template.

    Make sure you move the field item. If you remove the field, and add a new one later, most probably all the data will be lost.

    Also it's not recommended to build too complex inheritance structures. It won't be easy to maintain that in the future.