Thursday, January 21, 2016

Branches in Sitecore to automatically have child items

With Sitecore it is sometimes necessary for a given item template to have set children items for each instance. An example would be each template of web page type, having a child item of datasources type (which would be a folder with insert options for the data sources for any sublayouts on the web page item.

Instead of having to manually create the child item for each web page on the web site, the branches concept in Sitecore can be used to automatically add the child item when a parent item is created. Effectively a branch in Sitecore can be used to create a set of items rather than a single item.

To create a branch

  1. Create the parent a child(ren) templates as you normally would.
  2. In the content editor, under Templates > Branches create a new template of the branch type - /sitecore/templates/System/Branches/Branch
    1. Give this branch template a useful name as it will be used for insert options
  3. Under the branch template, insert a template of the parent type and give it a name of $name - this will ensure the parent is created with the name the users enters at insertion time
  4. Insert the child template under the parent template and give it a default name - datasources for example
  5. Assign this branch item as the insert option on the relevant templates instead of the parent item. This way you insert the group of templates instead of a single parent item.
In the example image above, "Web Page" is the Sitecore default branch template. $name is my custom page template (parent) and datasources is my custom datasources template (child).

No comments:

Post a Comment