I have seen in SXA that when I add a rendering to the page, it automatically creates a datasource item for the rendering when it adds it to the page. This helps the user not have to create a datasource item each time the rendering is added. Is there a simple way to accomplish this?
1 Answer
A pretty simple solution would be to replace the OOTB webedit:addrendering
command with your own custom command that automatically creates a datasource item at the specified datasource location. The command you are replacing is the below:
<command name="webedit:addrendering" type="Sitecore.Shell.Applications.WebEdit.Commands.AddRendering, Sitecore.ExperienceEditor" patch:source="Sitecore.ExperienceEditor.config"/>
Some considerations:
- You will want to prevent the Select Rendering Datasource dialog from popping up, since the datasource will have already been created
- To support multi-location Datasource Location field values, you may want to default your rendering to use the first specified location
- Anytime you use this command, you are forcing the author to create a new datasource without giving them the option to reuse an existing one, so you may want a way to turn that off or to limit your command to only run its custom logic for certain renderings.
- You may need to create a cleanup task for unused datasource items that were auto-created but never used, in case authors add renderings and then decide to change the datasource to an existing one