Wednesday, May 4, 2016

Sitecore treelist dynamic/relative datasource path

I was setting up a re-usable blog branch in Sitecore that contained the following structure:

  • Blog Home
    • 2016
      • 1
      • 2
        • Blog post
    • Categories
      • Category A
      • Category B
The blog post template had a treelist field which needed to have it's datasource connected to the categories element. Being a branch this needed to be dynamic to the branch itself, rather than hard coded to a specific path. This was achieved by setting the datasource to:
query:ancestor-or-self:: *[@@templateid = '{2D516207-AD30-4B71-9394-9A9E451C26D0}']/*[@@templateid = '{BC593CC9-213E-4466-AFB6-FDB089A449BA}']/*[@@templateid = '{8603348F-A6D4-45A4-9007-068E2E1C426B}']
Where:
  • {2D516207-AD30-4B71-9394-9A9E451C26D1} = Blog Home template
  • {BC593CC9-213E-4466-AFB6-FDB089A449BB} = Categories template
  • {8603348F-A6D4-45A4-9007-068E2E1C426B} = Category template
The end result was a tree list where the blog categories (category A and B) where able to be selected.

1 comment: