SitecoreBlog - navavayas.com
  • HOME
  • ABOUT
  • navavayas

Sitecore get all base templates for an item recursively

Tags:     sitecore-templates, sitecore

It's a good approach to divide all your Sitecore templates into small chunks. This helps reusability of the templates.

What I normally do is create a template called "page" derived from a standard template and all my page templates will be derived from Page.

similarly, I create a template called "content" derived from a standard template and all content templates will be derived from the content template.

Sometimes we need to find out all the base templates associated with a single template.

This is how we can achieve this:

TemplateManager.GetTemplate(currentVersionToPublish)
                        .GetBaseTemplates()
                        .Any(template => template.ID == new ID("{TemplateIDTocompare}"));

 

 

GetBaseTemplates method will return all bases templates for the template recursively.

 public TemplateList GetBaseTemplates()
    {
      List<Template> result = new List<Template>();
      Set<ID> processed = new Set<ID>();
      this.AddBaseTemplates(result, true, processed);
      return new TemplateList((IEnumerable<Template>) result);
    }

 

 

2nd parameter true in AddBaseTemplate specifies deep.

 

Hope this helps.

 

 

READ MORE

RECENT POSTS

  • Sitecore get all base templates for an item rec...

POPULAR POSTS

  • Sitecore get all base templates for an item rec...

CATEGORIES

  • sitecore-unicorn
  • sitecore-contacts
  • sitecore-xdb
  • sitecore-handling-500-errors
  • sitecore-bug
  • sitecore-media-library
  • sitecore-content-editor
  • sitecore-patch
  • personal-opinions
  • programming
  • sitecore-error
  • sitecore-usermanager
  • sitecore-languge-settings
  • sitecore-content-language
  • microsoft-Bot-Framework
  • Chatbot-Beginners
  • ChatBot-Emulator
  • botFramework
  • FirstChatBot
  • chatbot
  • sitecore-customroutes
  • sitecore-api
  • sitecore-buckets
  • sitecore-wildcards
  • sitecore-layoutdetails
  • sitecore-modals
  • sitecore-client
  • sitecore-deviceeditor
  • sitecore-best-practises
  • sitecore-templates
  • sitecore
  • sitecore-scheduling
  • sitecore-autopublish
  • sitecore-autopublish
  • extension-method
  • sitecore-multisite
  • sitecore-events
  • sitecore-publishing
  • sitecore8.1
  • Rendering Parameters
  • GlassMapper
  • Sitecore

Copyrights © 2021 Blog All rights reserved | Build by Lohitha Palagiri |Template by W3layouts