Too many times I’ve seen this question, it is not even funny. My images are not showing up when I publish an item. While it can easily be explained from the technical perspective and addressed, it does not always make sense from user’s point of view.

Today I’ve learnt that Sitecore 6.2 actually introduced a small feature, an additional processor for the publishItem pipeline, intended to process related media items linked from the FileDropArea field, which was also another addition in 6.2.

You may have already guessed. While it is only processing the links from FDA field, that does not mean we cannot teach it to process links from other fields as well.

using System.Collections.Generic;
using System.Linq;  
using Sitecore.Data.Items;  
using Sitecore.Diagnostics;  
using Sitecore.Publishing;  
using Sitecore.Publishing.Pipelines.PublishItem;  
  
namespace Project.Shell.Pipelines.PublishItem  
{  
 public class AddAllItemReferences : Sitecore.Publishing.Pipelines.PublishItem.AddItemReferences  
 {  
 protected override List<Item> GetItemReferences(PublishItemContext context)  
 {  
 Assert.ArgumentNotNull(context, "context");  
 var list = new List<Item>();  
 // calling base method which processes links from FileDropArea field  
 list.AddRange(base.GetItemReferences(context));  
  
 // adding our "own" related items  
 list.AddRange(GetRelatedReferences(context));  
 return list;  
 }  
  
 protected virtual List<Item> GetRelatedReferences(PublishItemContext context)  
 {  
 Assert.ArgumentNotNull(context, "context");  
 var relatedReferenceList = new List<Item>();  
 if (context.PublishOptions.Mode == PublishMode.SingleItem)  
 {  
 var sourceItem = context.PublishHelper.GetSourceItem(context.ItemId);  
  
 if (sourceItem.Paths.IsContentItem)  
 {  
 var itemLinks = sourceItem.Links.GetValidLinks();  
  
 // adding only valid related items  
 relatedReferenceList.AddRange(  
 itemLinks.Select(link => link.GetTargetItem()).Where(relatedItem => relatedItem != null));  
 }  
 }  
  
 return relatedReferenceList;  
 }  
 }  
}  
  

Now all you need is replace the default AddItemReferences processor with the custom one:

<!--<processor type="Sitecore.Publishing.Pipelines.PublishItem.AddItemReferences, Sitecore.Kernel"/>—>  
<processor type="Project.Shell.Pipelines.PublishItem.AddAllItemReferences, Project.Shell"/>

Too many blogs and articles were already posted about how to do it, but I still believe this is the easiest and most elegant way of solving this challenge.

http://sdn.sitecore.net/FAQ/Media/Publish%20related%20media%20items.aspx