Publishing Images of Datasource Items

One of the really important features that Sitecore should include is to publish the linked items to datasource items such as images when publishing an item with its related items.

I have been working on such a task to achieve this. Let me start by explaining the problem with an example:

My Home item has two items in its presentation details: a header rendering and a footer rendering. Each of these 2 renderings gets their data from datasource items. My header’s datasource item has a logo field of type Image. When I publish my Home item, Sitecore publishes the Home item itself only. If I publish my Home item with Related Items, Sitecore publishes the Home item with the header and footer datasource items. However, the linked items to the datasource such as the logo image do not get published, so the Home item would be missing items.

Of course, this is not a major issue, but may be inconvenient for the end user to keep publishing every linked item to the datasource items. Thanks to Sitecore’s flexibility, it is quite easy to do this.

What I did is create a new event handler for the ‘publish:itemProcessed’ event:

   <event name="publish:itemProcessed">
      <handler type="PublishItemProcessed class type" method="Execute" />

I created a helper class to publish the linked items as follows:

public class PublishingLinkedReferencesHelper
  public void PublishImageFields(Item itemBeingPublished, ItemProcessedEventArgs args)
     if (itemBeingPublished == null)

     //Image Fields
     List<ImageField> imageFields = itemBeingPublished
        .Fields?.Where(w => w.Type == "Image")
        .Select(s => (ImageField)s).ToList();

     if (imageFields != null && imageFields.Any())
       foreach (var item in imageFields)
            .Add(new Sitecore.Publishing.Pipelines.Publish
            .PublishingCandidate(item.MediaID, args.Context.PublishOptions));

This helper class would be called from the newly created event handler as follows:

public class PublishItemProcessed
  public void Execute(object sender, EventArgs args)
    var itemProcessedArgs = (args as ItemProcessedEventArgs);
    var itemProcessedID = itemProcessedArgs.Context.ItemId;
    var itemBeingPublished = itemProcessedArgs.Context.PublishHelper

    if (itemBeingPublished == null)

    if (itemBeingPublished.Paths.FullPath.StartsWith("/sitecore/Content/", 
      PublishingLinkedReferencesHelper helperService = 
        new PublishingLinkedReferencesHelper();
      helperService.PublishImageFields(itemBeingPublished, itemProcessedArgs);

And that should do it. If you publish the Home item with Related Items, the linked images should get published too. Of course, you can edit the above code to publish linked items and not just images.


Leave a Reply

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

You are commenting using your 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