Insights

Treelist In Sitecore Has More Options Than You Think

Showing, hiding and stopping users from selecting items

Beyond The Data Source

The Treelist and TreelistEx fields types in Sitecore present the content author with many options. They can choose anything they like from a given point in the Sitecore tree. While this freedom is great, too much freedom creates headaches for developer and compromises the health of the CMS.

While a fields Source property in Sitecore can be set by GUID, Path, XPath Query. Treelist and TreelistEx controls have a fourth very powerful option, the query string.

The Query String

Using all available options, this is what a Source using a Query String would look like:

datasource=/sitecore/content/fishtank/list&allowmultipleselection=no&databasename=otherdb&excludeitemsfordisplay=item1&excludetemplatesfordisplay=template2&excludetemplatesforselection=template3&includeitemsfordisplay=item2&includetemplatesfordisplay=template4&includetemplatesforselection=template5

All The Parameters

A little overwhelming. Let's break it down by parameter: (Case-insensitive, Sitecore forces values to lowercase internally)

DataSource

The root item the the field points to. Must be a path. This is short coming I believe to be addressed in Sitecore 7.
datasource=/sitecore/content/fishtank/repo

AllowMultipleSelection

Allows the same item to be added twice by the content author. Defaults to "no".
allowmultipleselection=yes

ExcludeTemplatesForSelection

A comma separated list of template names (no ids). Items are visible in the tree, but can’t be added/selected. My favorite.
excludetemplatesforselection=Item Name,Item Name 2

ExcludeItemsForDisplay

A comma separated list of item names or ids to be hidden from the author. Useful to hide unwanted folders, for example.
excludeitemsfordisplay=Item Name,{93FBD42C-458A-4C9C-A999-1EE2B651C6E1}

ExcludeTemplatesForDisplay

A comma separated list of templates names (no ids). Items based on these templates will hidden from the author.
excludetemplatesfordisplay=Item Name

IncludeItemsForDisplay

A comma separated list of item names or ids to be shown to author.
includeitemsfordisplay=Item Name,{93FBD42C-458A-4C9C-A999-1EE2B651C6E1}

IncludeTemplatesForDisplay

A comma separated list of templates names (no ids). Items based on these templates will shown to the author.
includetemplatesfordisplay=Item Name

IncludeTemplatesForSelection

A comma separated list of template names (no ids). Items are visible in the tree and can be selected.
includetemplatesforselection=Item Name

DatabaseName

Changes the database name being referenced. Useful if using an external data provider.
databasename=otherdb

This is the look of a Source field I'd typically set.

datasource=/sitecore/content/fishtank/repo&excludetemplatesforselection=Some Folder

I haven’t come across a real-world scenario to use the Include*** options but I know they're out there.

Although not perfect (i.e. names and paths instead of ids), the Query String field source adds functionality to the Treelist and TreelistEx that can lead to less code and a simpler authoring experience. And in the bigger picture, leveraging the Query String field source approach opens up many possibilities when creating your own custom fields.

Thanks for reading.

👋 Hey Sitecore Enthusiasts!

Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.

What’s in it for you?

  • Stay up-to-date with the latest Sitecore news
  • New to Sitecore? Learn tips and tricks to help you navigate this powerful tool
  • Sitecore pro? Expand your skill set and discover troubleshooting tips
  • Browse open careers and opportunities
  • Get a chance to be featured in upcoming editions
  • Learn our secret handshake
  • And more!
Sitecore Snack a newsletter by Fishtank Consulting
 

Meet Dan Cruickshank

President | Sitecore MVP x 11

Dan is the founder of Fishtank. He's a multi-time Sitecore MVP and Coveo MVP award winner. Outside of technology, he is widely considered to be a top 3 father (routinely receiving "Father of the Year" accolades from his family) and past his prime on the basketball court.

Connect with Dan