Recursive data source rewrite for Branch Templates

Functionality to resolve local rendering datasources when creating instances of said branch templates.

This is the first official blog post of the Decerno Sitecore tech blog! Long may it live. Let's jump right into the good stuff. In this case, the good stuff is Sitecore branch templates.

You should be using branch templates, all the cool kids are doing it

I like the concept of branch templates in Sitecore, it offers a very neat approach to paving the way for your editors by setting up predefined substructures that can be created in a one-shot manner. For this reason I've come to use them a lot, and in doing so I've discovered some technical limitations.

Local data sources

The big one for me was the inability to resolve local rendering data sources. I like the concept of local data source folders (used by, for example, the Sitecore Habitat project). In short, if a data source is only relevant and used within a certain substructure, it should be placed in a local data source folder in the root of that substructure, and not on a higher "global" level. This does not play well with branch items out of the box however. If you create local data source items in your branch item structure and set them as data sources on renderings of the branch item, they will not be resolved when you create an instance of the branch item, resulting in broken links in your rendering data source fields.

Example:

Example of local datasource value

Kamsar to the rescue!

Kam Figy (aka. Kamsar) is the guy behind some of the best sitecore tools out there, like Synthesis and Unicorn. When googling the local branch item data source problem, I found Kamsars BranchPresets project, in which he has solved the exact problem I had run into. By extending the addFromTemplate pipeline with an additional processor, the pipeline is now able to rewrite rendering data sources for the instance of a branch item, whenever one is created. This is really great, and in most cases it's all you need.

"But hey, what about local rendering data sources for child items of the branch item instance?"

That's exactly what I thought, uncanny... I needed the same functionality for the subitems of the branch template, since I had a ton of them, all with their own predefined renderings, some of which needed local rendering data sources.

So, i forked Kamsars BranchPresets project and rewrote parts of it, so that it handles all subitems recursively and rewrites their data sources as well when an instance of the branch template is created. Now, we have branch templates that can handle all sorts of data sources for their renderings, be it on the main item or subitems, placed locally or globally, as illustrated by the below example:

Local branch template datasource rewrite

I created a pull request towards Kamsars project, sat back and waited for all that sweet branchpreset-money to start rolling in.

Beaten to the punch...

Turns out someone else had solved the exact same thing a while ago, it just had not been merged into the master branch yet. So sadly I have not really contributed to the original project, but I still think it offers great functionality and if you are using branch templates and like keeping data sources in good order (as any sane person do), you should check it out! You can also choose to use my implementation, if you'd like to review the work that I did.

 

The Original Kamsar project: 

https://github.com/kamsar/BranchPresets

My implementation: 

https://github.com/BonNil/BranchPresets/tree/recursive-rewrite

 

16 Apr 2018, by Bonny Nilsson | 

Sitecore, Sitecore 9, Branches, Branch Templates, Datasources