Sitecore Branch Templates


This is one of the Sitecore templates to create the items but, 
  1. When can we use this template? and
  2. How to create it?

Well, the answer to your first question is, when your content tree has a set of items that follow the similar tree structure and fields present on those respective items then the Branch Template is a good choice and a time-saving technique to create the content.



Let’s understand this with a scenario.
Suppose you have to create the content like, the first level is a page (Quote) then the second level is a parent folder (_PageData) for the data source item (Datasource Item) and the third level is data source item (Datasource Item) for the first level item, as shown in the screenshot below





Now, if most of your pages have to follow the same structure then you don’t need to create always three items (page, folder, and Datasource item) separately with their corresponding template. We can take advantage of Sitecore’s Branch Template.

Coming to the answer to the second question how can we use this. To answer this, follow the below steps:

Step-1:

Note down what template in actual you require, for example, in our case we will create two (one for the page (Quote) and another for Datasource item). For the folder (_PageData), the template is already provided by Sitecore.

Step -2:
Create the noted templates with the required fields and renderings.


For example, I had created Title and Url fields on the Page Template. Title, Image, and Summary fields on the Datasource Template.

Step – 3:
Now to create branch template, right-click on /sitecore/templates/Branches/User Defined (or, you can create your own folder, I have selected User Defined for demo purpose only) à Select Insert à Click on New Branch

Once you click on the New Branch then a popup will be opened to select the templates, now you have to remember your content structure. In our content structure, our first level item is a Page Item (Quote) which should be created using /sitecore/templates/User Defined/Page Template so we will select Page Template, the screenshot below.


Step – 4:
Now our tree looks like the following:




Now remember, under the page (Quote), we will keep the data source folder ( _PageData), so for creating that hierarchy now select the $name and right click à Insert à Insert from Template à choose folder template from /sitecore/templates/Common/Folder, and name it as _PageData à click on Create .


Step – 5:
Under _PageData we should keep the data source item (to be created using /UserDefined/DataSource Template).

Now, right-click on /sitecore/templates/Branches/User Defined/Page Template/$name/_PageData folder à insert à Insert from template à Select Datasource template from /sitecore/templates/User Defined/Datasource Template and name it Datasource Item.



Once you follow all steps correctly then you should end up with following.


Till here, our branch template is ready to use, now we will use it to create the content, so let's move to Step-6:

Step – 6:
Right-click on home node à insert à Insert from template à Select our branch template (Page Template) from /Sitecore/templates/Branches/User Defined/Page Template à name the item as Quote à Create. 



Once you follow the steps then you can a set of items is created under the home node in the same hierarchy in which we have created our branch template.


NOTE: you can also use the Sitecore tokens in the field of the templates create under the /sitecore/templates/user defined folder.

Now when you need to create a new item with a similar pattern then you can create the set using branch template instead of creating one by one item by their corresponding templates.

Have any doubt? Reach out to me on prashant.tomar@hotmail.com

4 comments:

  1. https://sitecorepeanuts.blogspot.com/2020/07/sitecore-branch-templates.html

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thanks Prashant for giving an clear example of using branch template. Could you please extend the article by filling up the data-source value in rendering in presentation detail while calling branch template.

    ReplyDelete
    Replies
    1. Thanks, @Shailesh Sahu for going through the blog. As per the requirement, you are asking for this is not by default provided by sitecore to roll out the value in the data source field to point it to the relative data source item.

      But, with a few pipeline extensions, it can be achieved. For doing this we have two options:
      1. Create a custom token and use that custom token in the data source field of the rendering so that while creating the items those custom tokens can be replaced with their relative data source item.
      https://soen.ghost.io/working-with-custom-tokens-in-sitecore/

      2. Extend "RenderRenderingProcessor" to so that your query (e.g., query:.//*[@@templatename='Datasource Template']) passed in the data source of the rendering, to get the children of the current item with a specific template, can return the specific item.
      https://www.ghostwheel.co.uk/sitecore/queries-in-sitecore-rendering-datasources/
      http://sitecorepro.blogspot.com/2015/05/sitecore-query-in-mvc-rendering.html

      Hope it helps :-)

      In most cases, the 2nd option is chosen as convenient and less time taken.

      Delete

Creating Solr core for sitecore using command prompt

We setup the solr cores using command “C:\solr8985\sc103solr-8.11.2\bin>solr.cmd create -c sitecore_sxa_web_index -d sitecore_configset” ...