Publish Related Items & Defining Custom Relationships via Code – Sitecore

by Max Slabyak

related items

Version v7.2 Released

First and foremost, I’d like to congratulate the Sitecore Dev team on the new v7.2 release. I’ve been using Sitecore since v6.1 and since then, have developed a modest wish list of features I would have loved to see. I can honestly say, this version is jam-packed with features from that list.

Publish Related Items Feature – Explained

One of those new awesome features is the Publish Related Items feature. What. A. Timesaver.
Publish Related Items
It’s especially great for rapid development and deployment. Imagine you are developing a new feature.

  • You create the template.
  • You create the layouts.
  • You upload the media.
  • You create the content based on the templates, with the mapped layouts in the presentation layer and associate any media in the image files

You want to publish? Before v7.2, you had to publish each one individually. Not that rapidly deployed. Now, with Publish Related Items checked, all those assets associated with the content item you are publishing will get published, including any and all images contained within the content item, layouts, and templates.

Considerations

Keep in mind that this feature can get very performance intensive, so when you also have “Publish Subitems” checked, consider how many subitems you are publishing, multiplied by all the associated assets. It’s significant enough where Sitecore will actually warn you every time, so keep that in the back of your mind.

Roll Your Own!

This is one of the reasons I love Sitecore. You need something custom? Derive from a base class, stick that goodness in the config, and voila.

Suppose you have your own definition of what related items are. For my example, I would like to define a relationship for products. My products have cross-sell products (i.e. If you like product A, check out products B, C, and D). When publishing product A, if I enable publish related items, I would also to get product B, C, and D automagically published.

The Code

The example I’ll be describing is located in the snippets repository of my GitHub here – https://github.com/maxslabyak/snippets/tree/master/sitecore/PublishRelatedItems.

You will want to inherit your custom class from the Sitecore.Publishing.Pipelines.GetItemReferences.GetItemReferencesProcessor object. You will need to override the Process() method. The RegisterReferences() method is what adds the related items to be published into the context, which is a C# List of type Item.

namespace Sitecore.Custom.Publishing
{
public class PublishCrossSells : GetItemReferencesProcessor
    { 
 public override void Process(PublishItemContext context)
        {
            Assert.ArgumentNotNull(context, "context");
            List itemReferences = GetItemReferences(context); // this line calls the method from your custom class
            RegisterReferences(itemReferences, context); // this line registers the references to the related items.
        }
   }    
    }
}

So now, let’s talk about your custom method. You will want it to return a List of type Item. Since your related items are probably “related” to the context item – i.e. the item you are publishing – you will want to retrieve it so that you can run a query against it to get the said related items. You can do that simply with this line of code:

var sourceItem = context.PublishHelper.GetSourceItem(context.ItemId);

In my sample code on GitHub, I am doing a switch() statement looking for the specific product template. Remember, in the spirit of being as performance-aware as possible, we don’t want to run this custom processing logic for every single template; just the product item type.

While the switch statement is great in theory, if you are defining custom logic for multiple templates, I highly recommend you use the Decorator design pattern. This way, you follow the low cohesion/high coupling principle of OOP and don’t have to recompile and edit this class every time you have new logic or requirements. You can get a refresher course with my archived blog post here.

The looking up of the related items is just a simple Sitecore query that traverses the Axes, so I’ll spare you the Sitecore 101 code, but it’s posted in its entirety to my GitHub here.

Hooking into the Sitecore Config

As with any customization you write for Sitecore, there is a nice little .config file that needs to go along with it so that it can get hooked into the processing routines. Stick the following code in a .config file and put it in the App_Config/Include directory:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
         <getItemReferences>
            <processor type="Sitecore.Custom.Publishing.PublishCrossSells, Sitecore.Custom.Publishing"/>
          </getItemReferences>
     </pipelines>
  </sitecore>
</configuration>

And that’s it! Would love to hear your feedback to see how this feature is working out for you!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *