Search code examples
c#.netsitecoresitecore7

Sitecore FieldRenderer output differences between CMS 6.5 and 7.0


I've recently been moving content and codebase from an old Sitecore CMS 6.5 installation (with quite a number of customisations) to a clean installation of CMS 7.0.

In the original site a Single-line text field had been used to render arbitrary Javascript into the page (for tracking). This worked fine and the Javascript was rendered into the page, unencoded and executed as expected.

However in the new installation I have noticed that the content is now HTML encoded, which means that it does not execute under the Sitecore 7 installation I am working with.

In my Sublayout I have a FieldRenderer like this:

<sc:FieldRenderer ID="tracker" FieldName="Script" runat="server" />

I'm not sure if there is a customisation / config that I have not migrated across, or if this is a change which has been introduced in Sitecore 7 (possibly for secuity reasons).

Can anyone shed any light on this? Do I need to create my own field type to allow the desired behaviour, or is there a way I can do this with 'out of the box' field types?


Solution

  • I checked GetTextFieldValue processor class from Sitecore 6.5 and from 7 and looks different

    This is from Sitecore 6.5 : Sitecore.Pipelines.RenderField.GetTextFieldValue

     public void Process(RenderFieldArgs args)
       {
          Assert.ArgumentNotNull((object) args, "args");
          string fieldTypeKey = args.FieldTypeKey;
          if (fieldTypeKey != "text" && fieldTypeKey != "single-line text")
            return;
          args.WebEditParameters.Add("prevent-line-break", "true");
        }
    

    and this one is from Sitecore 7 : Sitecore.Pipelines.RenderField.GetTextFieldValue

     public void Process(RenderFieldArgs args)
    {
      Assert.ArgumentNotNull((object) args, "args");
      string fieldTypeKey = args.FieldTypeKey;
      if (fieldTypeKey != "text" && fieldTypeKey != "single-line text")
        return;
      args.WebEditParameters.Add("prevent-line-break", "true");
      args.Result.FirstPart = HttpUtility.HtmlEncode(args.Result.FirstPart);
    }
    

    you can see on last line of code on Process method from Sitecore 7 result is encoded. You can create your own class for GetTextField processor and add it to RenderField pipeline but I suggest you to change your field from Single Line Text to Multi Line Text or to Memo Field .

    I checked Sitecore.Pipelines.RenderField.GetMemoFieldValue class on both Sitecore 6.5 and 7 and implementation is same and the result is not encoded :

    namespace Sitecore.Pipelines.RenderField
    {
      /// <summary>
      /// Implements the RenderField.
      /// 
      /// </summary>
      public class GetMemoFieldValue
      {
        /// <summary>
        /// Gets the field value.
        /// 
        /// </summary>
        /// <param name="args">The arguments.</param>
        public void Process(RenderFieldArgs args)
        {
          string fieldTypeKey = args.FieldTypeKey;
          if (fieldTypeKey != "memo" && fieldTypeKey != "multi-line text")
            return;
          string linebreaks = args.RenderParameters["linebreaks"];
          if (linebreaks == null)
            return;
          args.Result.FirstPart = GetMemoFieldValue.Replace(args.Result.FirstPart, linebreaks);
          args.Result.LastPart = GetMemoFieldValue.Replace(args.Result.LastPart, linebreaks);
          args.WebEditParameters.Add("linebreak", "br");
        }
    
        /// <summary>
        /// Replaces the specified linebreaks.
        /// 
        /// </summary>
        /// <param name="linebreaks">The linebreaks.</param><param name="output">The output.</param>
        /// <returns>
        /// The replace.
        /// </returns>
        private static string Replace(string output, string linebreaks)
        {
          output = output.Replace("\r\n", linebreaks);
          output = output.Replace("\n\r", linebreaks);
          output = output.Replace("\n", linebreaks);
          output = output.Replace("\r", linebreaks);
          return output;
        }
      }
    }
    

    Code for GetTextField was updated on Sitecore 6.6 Update 3, you can see on release history:

    Page Editor In 6.6 Update-3, the pipeline was modified to HTML encode the field value when rendering single-line text fields (ref. no. 327905). This did not work correctly in the Page Editor which displayed the encoded value. And if the user saved the page, the already encoded value would be HTML encoded again. (384997)

    I hope it helps.