allBlogsList

Select the Associated Content for the Rendering

Sitecore provides us an option to add data source location for a particular rendering. For a multisite Sitecore solution, setting up a Datasource location to be a query will be very helpful as it resolves the location based on the site in which they are adding a rendering. In this article, I will discuss my experience and an issue I found with using queries for Datasource location...

While working through Experience Editor or Content Editor, when you add a rendering and browse for adding a Datasource,  a dialog opens up which allows us to use an existing Datasource or create a new one.

Scenario:

The query we built for the renderings looks something like this:

query:./ancestor-or-self::*[@@templateid='{4905A8C6-1BFF-4050-87F5-24F6DCD944CF}']/Shared/Datasources

I have customized Sitecore.Pipelines.GetRenderingDatasource.GetDatasourceLocation located in Sitecore.Kernel assembly to modify that query depending on where the user is adding the rendering.

Let’s consider, I am adding an Image Component rendering to an item with path /sitecore/content/Website A/Home/Products, the Datasource location query gets updated as

query:./ancestor-or-self::*[@@templateid='{4905A8C6-1BFF-4050-87F5-24F6DCD944CF}']/Shared/Datasources/Home/Products.

If a folder doesn’t exist, we will be creating a new one_._ Content author(s) now have an option to use an existing datasource or create a new one inside that folder.

Note: Every single folder for a page that gets created under Datasources is a custom template inherited from Folder template.

InsertImageComponentDialog

Recently, we received some feedback from the content authoring team that “Select the Associated Content” dialog was showing a wrong location for datasources.

I noticed that the issue was because a datasource and datasource location folder both had the same name. something like shown below.

DatasourcesStructure

Why is it pointing to an item with the same name and first in the list even though we explicitly defined in the code to use the folder?

After a lot of troubleshooting and no success, opened a ticket in Sitecore Support Portal. They have recorded this as a bug in the current version of Sitecore Experience Platform. The problem is that “Select the Associated Content” dialog uses item path to get the Datasource root item. Therefore, it always gets the top item with the given name.

If you notice the image above, we have two items named "Products" out of which Products folder is the one which is marked as the datasource location. However since the other item is the first in the list, it gets selected as Sitecore is resolving it based on path.

incorrect_datasource_location

Solution: 

This is expected to be fixed in the future versions of Sitecore. Until then, there are a couple of workarounds

  1. Move the Datasource folder above the item with the same name in the Content Editor.
  2. Rename your items, so that there are no identical items in the same level.

For now, we are using approach 2 and communicated with the authoring team not to name their datasources identical to page names.

This is an issue we noticed happening in both Sitecore 8.1 Update 3 and Sitecore 8.2 Initial Release.