CustomItemGenerator – Raw Vs Text value

Sitecore community is growing and there are so many market place modules, I came across one such gem from the market place “Custom Item Generator” will refer as CIG in the rest of the post, CIG creators also refer it as “CIG”. In this post I am not going to describe introduction of Custom Item Generator, how CIG can be configured? etc etc… All basic information related to CIG can be found at,

–          Marketplace CIG

–          Custom Item Generator

–          Custom Item Generator: A Simple Example

–          Custom Item Generator: Customization

Why I am writing this post?

Purpose of writing this post is to compare usage of FieldName.Text and FieldName.Raw properties.

I was facing this particular issue where I was not able to see rendered YouTube video in “Page Editor” mode. I was creating a sublayout that should render a YouTube Video and it should also be edited via page editor. The video was rendered in an iFrame where in the src attribute should point to the video on YouTube.

At first, I thought it would be a page editor issue and it might not render content from iFrame. So tried placing simple html iFrame tag without any dynamic attributes assignments from code behind and it was rendering the video correctly.

The same issue I was facing with items in the dropdown where in the <input> tags were rendered instead of the names of the items.

Custom Item Generator - 1

I debugged my code and with help of quick watch in visual studio found that FieldName.Text and FieldName.Raw both are returning different values. This made me interested as to why FieldName.Text provides html tag and not only the actual value.

Looking further at the documentation of CIG I found below explanation on CIG blog which suggested the usage of the .Text and .Raw properties.

  • FieldName.Text – This will access the field through the Sitecore field renderer, in most cases where you are displaying a field’s content on the front end you would want to use.
  • FieldName.Raw – This will return the underlying value that is being stored in the field.

Digging further and making use for RedGate Reflector I figured out that .Text calls indirectly FieldRenderer.Render.

public string Text
{
    get
    {
        return base.Rendered;
    }
}

Extract from BaseCustomField

public string Rendered
{
    get
    {
        if (this.field == null)
        {
            return string.Empty;
        }
        return FieldRenderer.Render(this.item, this.field.InnerField.Name);
    }
}

So the conclusion is if you want to render a general link field or an image field and would also like to have page editor support use FieldName.Text. If your requirements are content should not be edited via page editor as was in my case for dropdown and iFrame, make use of FieldName.Raw.

If you are interested reading further for the difference between raw and rendered values check this blog post “RAW VS RENDERED FIELD VALUES” written by Martina.

Leave a Reply

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