Raw vs rendered field values

What are raw and rendered values?

In Sitecore, there are two ways of getting data out of a field – the raw value or the rendered value.

A field’s raw value is how the data is stored in the underlying database – for simple fields such as Single Line Text, there is no difference between the rendered and raw values; it’s just plain text.

This is not the case for more complex fields types, such as General Link, Image, or Rich Text. Take the raw value of  a General Link field, for instance – if you enable raw values in the Content Editor (by clicking the View tab and ticking Raw values) and look at an item with a General Link field, you will see that the value stored in the database is custom XML:

<link text='My link' linktype='internal' class='' alt='Go here'  querystring='' id='{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}' />

This XML contains all the information required to construction a clickable anchor tag, but you clearly cannot output it to the browser as is.

How do I output raw values?

If you want to output the raw value of a field (which is perfectly acceptable for Reference fields, such as a droplink), you have two options.

The first method will either return a value or an empty string (including when the field does not exist). You will not get a null reference exception:

item["MyField"]

The second method will either return a value or throw a null reference exception if the field does not exist:

item.Fields["MyField"].Value

Note: If you are dealing with a field that does not contain content – such as a reference field, multilist field, or checkbox field – you may want to retrieve the field as a strongly typed object and use its methods. For example, the MultilistField object has a GetItems() method:

MultilistField m = item.Fields["MyMultilistField"];

var myItems = m.GetItems();

How do I ‘render’ field values?

Rendered field values are field values that have gone through some kind of processing before appearing in the browser. This only applies to fields that contain content – there is no need to render a multilist field (which is just a pipe-separate list of IDs) or a checkbox field (which is a boolean). To render a field, use the FieldRendered.Render() method:

string myRenderedValue = FieldRenderer.Render(myItem, "MyFieldValue");

The rendered version of our General Link field would be a standard <a href=”” />, with a friendly URL rather than an ID.

All Sitecore controls (such as <sc:Text />, <sc:Image />, <sc:Link />) eventually go through the FieldRenderer.Render() method, and will produce the same result.

Note: If you use XSLT, this also applies to sc:field() – if you want the raw value in XSLT, use sc:fld() instead.

Why should I use FieldRenderer.Render()?

The obvious reason to use FieldRenderer.Render() is that it transforms custom XML into HTML, as is the case for the General Link and Image field types. Other reasons include:

  • The act of going through FieldRenderer.Render() is what enables Page Editor support – you will not be able to edit field values that have been output as a raw values.
    • If you wish to disable Page Editor support for a particular field, pass a parameter in to the Render method (there is a corresponding attribute on the Sitecore controls that will do the same thing):
      FieldRenderer.Render(item, &quot;MyField&quot;, &quot;disable-web-editing=true&quot;)
  • Rich text field links and an images are stored with dynamic URLs (e.g. “~/link.aspx?_id=3454C251162749BCA64E2495B3EAB0C5&amp;_z=z”)  – this is great when items are renamed or moved because it prevents broken references, but they are not SEO-friendly. The FieldRenderer transforms these IDs into paths.

What about MVC?

There is an MVC-style helper that also goes through the FieldRenderer.Render() method:

@Html.Sitecore().Field(item, &quot;MyField&quot;)

You can pass parameters into into the .Field() method in exactly the same way.

3 comments

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s