7

Trying to set datasource for a treelist field in Sitecore. Need to set two different folders as path.

DataSource=/sitecore/content/home/folder1|/sitecore/content/home/folder2

It does not work, it sets home node as starting path.

Also tried IncludeTemplatesForSelection=template1,template2

Datasource=/sitecore/content/home&IncludeTemplatesForSelection=template1,template2
6

I can think of two options to solve your issue:

  1. Create some custom code to make a "query" possible together in the datasource. Luckily for you that is already been done and well documented. Unfortunately as Jammykam correctly pointed out, this won't completely fix your as the TreeList can only accept one parent node. So this would actually come down to a complex version of option2. You could however try to write your own field (based upon a treelist), but that might take some more customization. But you're lucky again! There is another Kam, and he shared some code to do exactly that: a multi-root treelist.
  2. Use the ootb options, with one path but by using the IncludeTemplatesForDisplay and IncludeTemplatesForSelection. This might get a good result, but it depends on the templates used in folder1 and folder2. Assuming your template1 and template2 are used for folder1 and folder2 (and no others on that level), you could set Datasource=/sitecore/content/home&IncludeTemplatesForDisplay=template1,template2,datasourcetemplate&IncludeTemplatesForSelection=datasourcetemplate. Note that I included datasourcetemplate (the template of the source you want to be selected) in templates to display and to select (to make them visible and selectable).

Remark: option "1" was edited after jammykam's comments (thx for the correction). I'll kept part of the text to make the comments not completely weird. And then shifted the solution from one kam to the other :)

  • 1
    Option one will not work with a Treelist control, since it only supports a single item as root node. Supplying multiple item list would require customisation to the control. – jammykam Nov 27 '16 at 17:18
  • Must admit I didn't try it, but your blogpost is titled "Specifying query and parameters for Sitecore Treelist Field Source"? (I did mention custom code was necessary though) – Gatogordo Nov 27 '16 at 18:33
  • 1
    Yes but that blog post is solving a different problem (xpath query and template restrictions). The issue is the treelist control can only have one root node, unlike multilist for example which is a flat structure. So no idea how you solve specifying multiple source root nodes in this instance... – jammykam Nov 27 '16 at 19:30
  • 1
    Ah ok, now I see what you mean ;) And you're right - I'll adapt the answer. – Gatogordo Nov 27 '16 at 19:36
  • I actually wrote a blog post back in June that combined the Kam Figy and JammyKam posts that you linked to and does exactly what the OP is asking for. Both of those posts were great references! +1 and here is the post I wrote, for reference: zacharykniebel.com/blog/sitecore/2016/june/06/… – Zachary Kniebel Nov 30 '16 at 20:36
2

I actually wrote a blog post on this same issue a while back, and my solution combines both JammyKam's and Kamsar's solutions (the ones linked to in @Gatogordo's post) and implements exactly what you are looking for and more.

Note that I am currently using the code in my post on a production site.

Overview

Sitecore's Treelist field is an excellent way to select items that live under a common root node, but it doesn't support multiple roots. To support multiple roots, you will need to create your own field type.

My post will show you how, and includes support for Datasource Parameters (Include/Exclude from Template/Display, etc.) and also adds support for using Datasource Parameters with Sitecore Query (which isn't supported OOTB). It even allows you to mix and match query and non-query datasources, e.g. /sitecore/content/home/foo/bar|query:./[@@templateid='{...}]. In addition, it will also show you how you can override the display of the field to show the path to each item in the selection pane, in order to better support multiple roots with the same name without confusing the content authors.

Considerations

Before you start developing, there are a few things that you need to consider:

  1. If you're displaying multiple roots then you need to customize the view so that the content author can see the paths to the elements. Since roots may have the same name, the name of the root may not be meaningful enough.
  2. The default Treelist shows the root /Sitecore node if the root was not found. However, since we're going to be showing multiple roots, it's likely better to instead hide the node if the root was not found.
  3. If you're planning to use this on a solution with content search and you want to index this field type, you will need to add it to your index configuration.

Details

My post is too long to repeat everything hear, but basically what you need to do is override the logic for parsing the datasource and applying the datasource parameters, and then use a derived version of the MultiRootTreeview class to render the field. See my post for more details, the code, and thorough explanations of the different pieces that need customization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.