List Component Strategies & Publishing Data Source Child Items as Related Items

There are generally three strategies to building what I call "List components", components that display collections of things: Carousels, Accordions and Tab Listings are examples of these types of components.

Strategy One: Multi Lists Fields on Parent Template

The most straightforward way is to use a multi list or Treelist and allow content authors to select which items they want to display and what order to display them. This gives you the ability to have children shared across components and can lead to some usability issues as child item organization deteriorates and you end up with content that is no longer used anywhere.

Strategy Two: Composite Components

Many "Component Library" frameworks take this approach and separate the container of the list from the item in the list, providing separate components that can be nested in the parent components placeholders.  This gives the most flexibility as individual list items can be AB Tested and personalized, but this makes it difficult to reuse entire component configurations from page to page as it needs to be built up where ever it is used. "Snippet components" can help with this, but may be difficult for content authors to use effectively.

Strategy Three: Insert Options and Child Items

A third strategy is to have the child template as an insert option of the parent template and have the component read child items under the data source. This can be made very intuitive for content authors as Sitecore has many out of the box custom experience buttons for inserting, moving and sorting child items.

Making Strategy Three Work

The big problem with Strategy Three, is that children are not automatically included as related items, when publishing. So if you're publishing a page that references a data source that is the data folder for your site, it's child items will not get published even if you check related items and sub items, as the child items are not sub items of the page you're publishing, but of the data source it references.

Luckily for us, Sitecore is very extensible and gives us the ability to extend the "Get Item References" process using a pipeline processor. I've recently added an extension to Sitecore Foundation to make it easier to support strategy three with a pipeline processor named "PublishDataSourceChildItems."

This processor will check to see if the published item inherits from a new base template "_Base Publishing Children as Related Items", which is an empty template created to support these scenarios. This means if you're building components that expect child items to be published with it, they will need to inherit this template to tell our new custom processor to publish its children.

Please note that while I originally build this processor as recursive (to include children of children), I commented the recursive part out, assuming that child items that have child items could also inherit the base template if that was the desired behavior.

Code right now is committed to the SxA branch of Sitecore Foundation, though there is nothing in this code specific to SxA. Will eventually merge back to the main branch, but feel free to pull the code specifically if you need it.

Sitecore Foundation - Generate Dynamic PDF's based on Sitecore Content

Sitecore Foundation for SxA - Cshtml View Variants