Relinking Datasources from Branch Templates

Another nice trick I learned about from my last project which I know I really like and did not want to forget about was how after some research, we leveraged a processor to re-link datasources from a branch template to the newly created branch item set in the content section of Sitecore’s content tree.

This Can Be Tedious…

If you are not sure what I am referring, let me start with explaining the issue at hand.  Branch templates are great structures for creating multiple items at the same time.  This can especially be true, if as part of the branch template, you can also create datasource items which are designed to work with components of the main branch item so that those items already exist when the branch template is used.  However, after the new branch item set is created, the components in the presentation details of the newly created main item point to the datasources on the branch template in the template section and not the newly created branch item’s datasource item.  An image providing an example describing how that mechanism works by default is shown below.

2018-05-22_Sitecore_Default_Behavior_Corrected

Therefore the content author has to access the presentation details of the newly created branch item and re-assign each datasource to each component which can be quite tedious every time a new item set is created.  Over the long haul, it can be mind numbing to have to re-assign each and every datasource to a component for each item created from that branch template, so there is definitely motivation to find a way to make this process easier on content authors.

A Solution…

So we wrote a processor to evaluate items created from a branch template which takes the newly created datasources and assigns them to the components they belong to based on the original set-up from the branch template.  An image of an example which demonstrates what this update does is shown below.

2018-05-22_Relink_Datasource_Processor_Update

The Configuration File

Since this update involves implementing a new processor, it has to be added using a configuration update.  An example of a configuration patch file which can be used to include this update is shown below.  The processor XML line type attribute points to the class followed by the DLL or Assembly name.  If placed within the Sitecore’s root App_Config/Include folder, the patch changes are applied to the Sitecore environment.

2018-05-22_RelinkBranchTemplateDatasourcesConfig

The Processor Code

The processor code which was put together for this mechanism is shown below.   The first image begins with main processor class called by the configuration file above when published to Sitecore’s Website/bin folder within a DLL.  In this example, the class is called RelinkBranchTemplateDatasources and inherits the AddFromTemplateProcessor class.

2018-05-22_RelinkBranchDatasourcesProcessorBase

The main Process method is shown below…

2018-05-22_ProcessMethod

The RewriteBranchRenderingDataSources method is listed here…

2018-05-22_RewriteBranchRenderingDataSources

The ApplyActionToAllRenderings method is listed here…

2018-05-22_ApplyActionToAllRenderings

The ApplyActionToLayoutXml method is listed here…

2018-05-22_ApplyActionToLayoutXml

The RelinkRenderingDatasource method is listed here…

2018-05-22_RelinkRenderingDatasource

This is the complete code to perform the mechanism which is used to re-point the datasources used by the component of the newly created branch item from the original branch template location to the location of the newly created set of branch items.

One Unsolved Issue…

There is one situation, where this will not work as expected.  It is common when creating a branch template to use tokens like “$name” to dynamically assign names to newly created items.  If done at the root item (like image below), that is fine but if that token is used on one of the datasource items (as shown below as the child item of the Content folder), the processor will have difficulty properly translating and assigning this datasource to the proper component once the branch template is used.  So an activity I hope to work on is to solve this issue in code, so tokens can be used through out the branch template structure.

DOL Name Exception

Otherwise, if you hard code the datasource item names instead of using tokens, the process works just fine as discussed earlier in this blog.  I hope you find this blog useful.  If used properly, this re-linking processor can save your content authors quite a bit of time if they leverage branch templates to create the items of the content infrastructure.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s