torsdag den 27. november 2014

The Sitecore Source Property Explained - Part 1: Root Item

All Sitecore template field types have a source property. While this property isn't always used, it is a really powerful tool for filtering the data shown in various field types and improving the general user experience.



Sitecore supports three techniques for utilizing the source property:
  • Root Item
  • Treelist Parameters
  • Sitecore Query
Which of these techniques you should use, depends on the situation and the field type you are working with, as not all field types support the same techniques. 
In this blog post, setting a root item will be covered. The treelist parameters and Sitecore query will be covered in later blog posts.

Setting a root item

Defining a root item is supported for the following field types:
Simple Types: File & Image
List Types: Checklist, Droplist, Grouped Droplink, Grouped Droplist, Multilist, Name Lookup Value List, Treelist & TreelistEx
Link Types: Droplink & Droptree

Some of these field types require a root item to work. Others will by default let the user access the entire content tree or media library... or at least the parts, that the user is allowed to access. This might be fine for some cases, but setting a root item can be of great benefit to both the developer and the end user.
By setting a root item you can limit the users access to a certain part of the content tree. In doing so, you can not only potentially save the user a lot of clicks, you can also help prevent the user from selecting content of the wrong type. Eg. if you have a treelist for selecting featured blog posts on your front page and all your blog posts are located under the same parent node (Blogs) in the content tree. Setting the root item of the treelist to the Blogs-element, would prevent your users from having to navigate to the Blogs-element every time they need to select a new featured blog post.

Root item as path or ID


You can set the root item either by entering the item path or the item ID as the source property of the template field. You can get both the path and the ID from the Quick Info section in the Content Editor.

If the Quick Info section isn't visible in your Content Editor, you can enable it by following these simple steps:
  1. Click the Sitecore logo in the top left corner of the Content Editor
  2. Chose Application Options from the drop down menu
  3. Check the Quick Info Section-check box
  4. Click Ok
To set the root item as the source property for a template field, simply copy either the path:
/Sitecore/Content/Home/Blogs
or the ID:
{9FE94452-1CB3-4E45-B422-71D4F8CA9025}
into the source property.
There are pros and cons of doing this either way. The path is sensitive to the renaming and moving of content, as both of these actions would change the path. Using the ID would solve these issues, but if an item is deleted and replaced with a new item with the same name and location, the ID would no longer match, whereas the path would.

The effects of defining a root item for different Sitecore template field types


For the end user, the effect of setting a root item on a field, depends on the template field type.

For treelist field types, such as Droptree, File, Image, Treelist, and TreelistEx, the user will be able to select a descendant item of the root item.

For single-level list field types, such as Checklist, Droplink, Droplist, or Multilist, the user will be able to select only items that are direct children of the root item.

For grouped list field types, such as Grouped Droplink and Grouped Droplist, the user will be able to select items that are grandchildren of the root item. The direct children will function as the groups in the list.