Return to doc.sitecore.com

Valid for Sitecore 5.2, 5.3
How are selection fields populated?

Q:

How are selection fields populated?

A:

In addition to keying text in it is often necessary for users to make selections such as from predefined lists when performing content entry. Selectors for simple on/off, yes/no or true/false conditions can generally be implemented with a field of type Checkbox. More complex selectors should be implemented with other field types.

The items used to populate the lists typically reside in Sitecore. For instance the colors for a color selection field may be created under a path such as /sitecore/content/global/colors. Simple items such as colors can be based on Sitecore's default Folder template; those which require field values may be associated with custom templates. Any selection field other than checkbox should have its Source property set to control where the field will retrieve its list of values (see Specifying the Source). Note that different types of fields use the source property for different purposes, for instance the HTML field type uses this property to define the path to an HTML editor profile (see Customizing the Editor Using Profiles). The various types of fields and their implementations of the Source property are defined under Standard Data Types.

Usually a Sitecore path such as /sitecore/content/global/colors is used but more advanced logic requires Sitecore Query (see Using Sitecore Query). It's also possible to populate selectors with data in external repositories using Data Providers but this is out of the scope of this document (see Integrating External Data Sources); in this case the Source property still references a Sitecore path but that item is populated by a data provider rather than Sitecore native data.

Selector field types which support a single selection (at least Lookup and Tree) typically store the GUID of the selected item(s), although ValueLookup instead stores the names of the selected items, which can be convenient in some cases (especially when a simple list is only used for one purpose). It is possible to populate selector fields with field values in the listed items instead of their names; see Populating Lookup Fields with Field Values.

Selector field types which support multiple selections (at least Checklist, MultiList and TreeList) always store a pipe-separated list of the GUIDs of the selected items with no trailing pipe. Techniques described for working with MultiList fields should work for any field type storing a GUID or pipe-separated GUIDs (see MultiList).

For the simple case of colors, assume that every page can be associated with a color and that certain renderings will only display pages with a certain color selected. First create the folder path /sitecore/content/global/colors if it does not already exist. Within this folder create additional folders for the various colors. The default folder template can be used because colors are not expected to have additional properties - they have only their name, though Sitecore will automatically store system information such as key (lowercase of name), creation date and GUID which may or may not be useful to the application.

In each of the templates for which items should be associated with a color, add a field named color of type ValueLookup and set its source to /sitecore/content/global/colors. Then to iterate over the children of an item which have a specific color selected:

<xsl:for-each select="$sc_item/item[sc:fld( 'color', . ) = 'Red']">

Using GUIDs adds a layer of complexity. Instead of using ValueLookup, use Lookup as the field type for Color. If the field was previously implemented as a ValueLookup, changing it to a Lookup will invalidate all items with something selected in this field so they will have to be updated. The code for iterating over items of a specific color would then be:

<xsl:variable name="red" select="sc:item( '/sitecore/content/global/colors/red', . )" /> <xsl:for-each select="$sc_item/item[sc:fld( 'color', . ) = $red/@id]">

Reversing the relationship - for instance determining all pages that have Red selected as the color anywhere in the content repository without iterating over the entire dataset - is typically done with the Link Database. Sitecore may not maintain the link database for all field types - of the types referenced in this document, the link database supports only Lookup, Checklist and Multilist field types. Note also that Sitecore maintains the link table in the master environment; the master link database table can be used on the published site or link database tables can be managed in additional databases.