Referencing local data sources using ID instead of path
In my previous blog post we discussed about the problem with renaming local data sources and the issue with converting data sources created in a hierarchical structure to IDs using the out of the box PowerShell script. In this post I am going to show how to prevent using path to reference local data sources in the first place.
Sitecore decides on whether to use ID or path to link data source to the rendering in presentation details at the time of selection of data source using the Select Associated Content dialog.
So, I decided to take a look into what's happening behind the scenes when you click OK on this dialog using the network traffic monitor in the browser developer tools. I noticed a call to the following URL with the below response.
https://demo.dev.local/sitecore/shell/default.aspx?xmlcontrol=Sitecore.Shell.Applications.Dialogs.SelectRenderingDatasource&...
The response has a command called SetDialogValue with the path of local data source as value. The same call has different response value for the SetDialogValue command when I selected a shared data source. Instead of path I got ID of the selected data source item in response.
From the request URL I found that Sitecore.Shell.Applications.Dialogs.SelectRenderingDatasource is the XML control responsible for handling this request, so I decided to take a look into the control located at
<SitecoreRoot>\sitecore\shell\Applications\Dialogs\SelectRenderingDatasource\SelectRenderingDatasource.xmlIf you are using SXA, this dialog control is overridden, so you can find the control at the following path.
<SitecoreRoot>\sitecore\shell\Override\Applications\Dialogs\SelectRenderingDatasource\SelectRenderingDatasource.xmlTaking a look into the XML revealed the code beside class used by the control
To override the default behaviour I created a custom class inheriting from Sitecore.XA.Foundation.LocalDatasources.Dialogs.SelectRenderingDatasourceForm and overridden the SelectDialogResult method to always return the ID for both local and shared data sources.
Now to make the SelectRenderingDatasourceForm to use our custom class, make a copy of the <SitecoreRoot>\sitecore\shell\Override\Applications\Dialogs\SelectRenderingDatasource\SelectRenderingDatasource.xml file and copy it to <SitecoreRoot>\sitecore\shell\Override\SelectRenderingDatasource.xml. Open the file in any text editor and change the Code beside tag value to our custom class.
If you find this article useful or if you have any questions leave a comment in the comments section.
Comments
Post a Comment