Monday, September 27, 2010

Image Field in Page Editor [Common Errors]


This issue is related to the latest *6.2 rev.100831 (Update-4)* only.

You may be getting the following error when launching Page Editor on a page with an image field rendered via <sc:image /> Web or XSL Control:

at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName)
  at Sitecore.Resources.Media.MediaProvider.GetMediaUrl(MediaItem item, MediaUrlOptions options)
  at Sitecore.Xml.Xsl.ImageRenderer.GetSource()
  at Sitecore.Xml.Xsl.ImageRenderer.Render()
  at Sitecore.Pipelines.RenderField.GetImageFieldValue.Process(RenderFieldArgs args)
  at (Object , Object[] )
  at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
  at Sitecore.Web.UI.WebControls.FieldRenderer.RenderField()
  at Sitecore.Xml.Xsl.XslHelper.image(String fieldName, XPathNodeIterator iterator, String parameters)

While our team is working on an official document about, here is a quick 3 step solution:

1. Create a custom ImageRenderer class:

using System;
using Sitecore.Data.Fields;
namespace SitecoreSupport.Shell
{
   public class ImageRenderer : Sitecore.Xml.Xsl.ImageRenderer
   {
      protected override string GetSource()
      {
         var innerField = Item.Fields[FieldName];
         var imageField = new ImageField(innerField, FieldValue);
         ParseField(imageField);
         if (imageField.MediaItem != null)
         {
            return base.GetSource();
         }
         return String.Empty;
      }
   }
}

2. Create a custom implementation of GetImageFieldValue processor in the renderField pipeline:

namespace SitecoreSupport.Shell
{
   public class GetImageFieldValue : Sitecore.Pipelines.RenderField.GetImageFieldValue
   {
      protected override Sitecore.Xml.Xsl.ImageRenderer CreateRenderer()
      {
         return new ImageRenderer();
      }
   }
}

3. Create a .config file with the following content and place it under /App_Config/Include:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
   <sitecore>
      <pipelines>
         <renderField>
            <processor type="Sitecore.Pipelines.RenderField.GetImageFieldValue, Sitecore.Kernel">
               <patch:attribute name="type">SitecoreSupport.Shell.GetImageFieldValue, SitecoreSupport.Shell</patch:attribute>
            </processor>
         </renderField>
      </pipelines>
   </sitecore>
</configuration>

1 comments:

Anonymous said...

Thanks for posting; I was able to use this information to install a Sitecore support patch without having to modify web.config directly.