Local Datasources marketplace module
On the Sitecore marketplace a new module was recently added to help editors with the concept of "local" datasources. The module can be found here.
The source code is available on GitHub. Instruction on installing and using the module are included in the readme file.
We assume people working with Sitecore and reading this know what datasources are. Some of you might even work with a "local" datasources. Local meaning that the datasource item is coupled with the "page" item where it is used in a hierarchical way by storing the datasource item underneath the page item - in a "local" data folder.
We tried to automate this proces for datasources that should not be shared amongst "pages" while working in the Experience Editor.
The solution now consists of 2 parts.
- The first part will create the actual datasource item (and the data folder if that does not yet exists - datafolder will be pushed as latest child). The template name of the required datasource is used as base for the item name, combined with a number.
- A second part will prevent the "Select the associated content" dialog from appearing and is discussed already in a previous post: Preventing the Sitecore "select datasource" dialog.
I wrote another blog about the functionalities of the module - here I will go deeper into the code.
Creating the datasource
Creating the datasource item is done in a processor in the getRenderingDatasource pipeline. This pipeline is started within the AddRendering command (see step 2 - the dialog).
We check the datasource location of the rendering and determine whether it is "local". If so we start the item creation:
- Create the parent item for the datasource item if it does not exist
- based on a provided template
- underneath the context item, and pushed down to the last child position
- name is based on the datasource location (we should add some more data verification here)
- Add this parent item to the datasource roots
- Create the datasource item underneath the parent
- based on the template set on the rendering as datasource template
- using this template name of the datasource as base for the item name (combined with a number)
- Set the datasource item FullPath as CurrentDatasource in the pipeline arguments
A few things to notice here:
- We use a SecurityDisabler to make sure the item can be created.
- We use a SiteContextSwitcher (to "system") to make sure the page does not refresh (if you don't do this, the page detects a new item and refreshes - causing errors)
The datasource dialog
You can read here how we prevented the dialog from appearing and still setting the datasource value when the arguments were set correctly in the previous step using a custom AddRendering command.
As we do override the AddRendering command, the module needs a Sitecore version with the exact same code as the one we used in that command to keep all renderings without local datasources to work as expected.
The Sitecore Experience Accelerator (SXA) has it's own version of the AddRendering command - for the same reason btw.
This means that this module should not be installed on instances were SXA is also present!
A future enhancement could be to add SXA support to the module, although it might be a better option to try to use SXA functionality in your non-SXA site - but that is a whole other story...
Future enhancements
We released this module as version 1.0. It is working on Sitecore 8.2 and has some functionalities already but we are aware that there are still quite some areas of improvement. As mentioned here before, we might need to add some extra data checks to be more robust. But also on a functional level we still have ideas:
- a solution for renderings that are already set on the standard values of a template (using branches maybe?)
- remove datasource items if the rendering is removed (and no other links are found)
- content editor support? (although we're not quite sure we actually want this)
Feel free to use the module, share more ideas for enhancements, contribute to the code...