Have you ever been in the situation where you would like to point the “Source” field of template field to an item that will be used with a List Type field such as the MultiList, but you wanted that field to be pointed to a folder in the site the user is currently visiting?
One reason you may want to set a “Source” field to an item in the context site may be because the item you are creating is intended to be used with the DMS (Digital Marketing System) for personalization, A/B, and multivariate testing. We can get solid results in those areas with a data source component-based architecture where components can be conditionally rendered across pages in the same site or even across all sites in the Sitecore instance.
Anyways, an easy way of setting up a “Source” for a MultiList is simply just to point the “Source” field to the /sitecore/content/ item and let the user drill down into the folders to get to the folder they want. However, depending on how many sites the Sitecore instance has, in addition to the amount of content, this will turn out in most cases to be a bad user experience, and could lead to error where the user may accidentally choose items from another site.
A better solution is to use Sitecore query to set the “Source” field in the template field. If you are not familiar with the Sitecore Query or Sitecore Fast Query, I would recommend visiting the Sitecore Developer Network and reading up on it. The most recent Sitecore Query documentation I found was here: Sitecore Fast Query documenation. You will find it’s not that difficult to grasp and you can start using it right away once you see how the useful the queries can be in your day to day Sitecore development. In addition, Sitecore was kind enough to provide us Developers with the XPath Builder available in the Developer Center application via Tools –> XPath Builder. Below is a screen shot of the XPath Builder application:
All you need to do is set the Context Node to the item you want to start with for the query, build your XPath expression in the next field, and hit the Evaluate button and you will see the results in the Result field. Using this tool you don’t have to guess if your query is correct when you insert it in the “Source” field. Build it here, test it, then just copy and paste it into the “Source” field (along with “query:” added to the beginning of the query). That simple!
In one of my projects, I needed to point to a data folder in the context site that had the items I needed, as children, that I wanted to show in my MultiList. In the example, below I wanted to set the “Source” field in my ScriptSelectionItem to the ScriptItems folder for the context site so the user would only see the ScriptItems created for that site only and the user can just select the ones they want. Again, we want to get away from drilling down into the site specific folders and then making selections to create a better experience for the user and reduce errors.
Below is my query that I built and tested in the XPath Builder to start with the ancestor-or-self of the context item and then match up the template ids of the items once the initial ancestor-or-self item is established:
./ancestor-or-self::*[@@templateid='SITE TEMPLATE ID']//*[@@templateid='DATA TEMPLATE ID']//*[@@templateid='SCRIPTS TEMPLATE ID']//*[@@templateid='SCRIPTITEMS TEMPLATE ID']/*
When you add the query to a “Source” field you need to add the “query:” to the front of the query as such:
query:./ancestor-or-self::*[@@templateid='SITE TEMPLATE ID']//*[@@templateid='DATA TEMPLATE ID']//*[@@templateid='SCRIPTS TEMPLATE ID']//*[@@templateid='SCRIPTITEMS TEMPLATE ID']/*
Now, when I add the query to the “Source” field in my ScriptsSelectionItem template field and open the ScriptSelectionItem in the Content tree, I now see that the MultiList field points to only the items in the context site for the user. I have not gotten this to work with the DropTree List Type but the MultiList has worked just fine for my purposes. Happy coding!
Now for Site A:
ok . so each site has seperate templates like – “‘SITE A TEMPLATE ID” and ‘SITE B TEMPLATE ID ?
What if we have only one template for the data item and which then needs to point to respective node based on which context.site ..
No, sorry if there was a misunderstanding or if I mistyped my post. You will only have one TemplateID for all sites. In this case you would put in the datasource something like this: