Datasources are used for various reasons and at various places. There are mainly 2 types of Data sources.
These Datasources are one we discussed while discussing the field types. In the templates area each field type has a text box as Source. Some field types require a value in it and some don’t require. All the fields that display a list of items for the selection are using field Datasources.
These types of Datasources are known as Field Datasources and are widely used.
This type of Datasources we have not yet discussed in our document. We will go through it but before that let’s discuss a scenario where this datasource will be every useful for us.
Let’s consider we need to use the list of fruits at various places in our website. So creating those many copies of fruits folder is possible but how we will manage any modifications in it? So in this case we keep it at central location and pass it to the page as a datasource in a particular rendering. Secondly if we are hardcoding the logic of fetching the fruits then we will not be able to personalize the content based on the Personalization rules. So instead of hardcoding the logic, we should read the datasource and display the content.
Let’s use the rendering datasource to display the list of fruits in the Controller Rendering instead of Hardcoding it.
- Go to Visual Studio.
- Open ControllerRendering controller.
- Modify the code to fetch the list from datasource and not directly from the item.
- Comment the previous logic. And add the new logic.
- Publish the Solution since there is code behind changes.
- Go to Sitecore and Open Content Editor.
- Navigate to the Test Item.
- Click on Presentation tab >> Details.
- Click on Final Rendering >> Controller rendering.
- Click Browse under Datasource field and select the Fruits folder.
- Click OK and publish the Test Item.
- Browse http://learningsitecore/test-item
- The front end remains the same but now the content editor can select the folder from Sitecore itself and the data will be displayed based on the datasource provided.
The rendering datasource can be defined at 3 locations – On item’s rendering (the one we did), Templates Standard Value’s rendering and at the Rendering. Since we can define at 3 locations, it has fallback mechanism. First it checks for Item’s rendering datasource >> Standard Value’s rendering datasource >> Rendering. If none of these 3 has datasource value then it will be blank displayed on Front end.
Let’s see how we can quickly change the content using datasource without any code changes. We will display the list of Colors instead of Fruits.
- Go to Sitecore >> Content Editor >> Test Item >> Launch Experience Editor.
- Go to View >> Check Designing checkbox. Click on Controller Rendering part.
- Click on the drop down arrow on the 5th option >> Change associated content.
- By default fruits folder will be selected as it is our current datasource. Select Color folder and click OK.
- And the content is changed.
- This is the power of Rendering Datasource. It is highly recommended to use as much as datasources as possible so that you can personalize your website based on it.
Now since the content editors have the ability to choose the datasource, it increases the chances of error. To minimize the error, we should restrict or narrow down the available selections.
- Go to Content Editor and Navigate to Controller rendering (/sitecore/layout/Renderings/Controller rendering)
- Scroll down To Editor Options section and find the field ‘Datasource Template’.
- Click Insert link.
- Select Folder template as we select folders as our datasources in our examples.
- Click Insert. Save the changes.
- Switch to Experience Editor and Reload it.
- Click on Controller rendering area >> Click dropdown arrow next to the 5th option >> Change associated content.
- By default Color folder is selected as it is our datasource. Click on any other item which is not a folder.
- From the above image, you can see that an error message is displayed to the Content Editor and OK button is disabled. This reduces the error of selecting the wrong datasources.
- If you select any of the folders (Color, CssClass and Fruits), you will be allowed to select it as a datasource.
In this blog, we have learnt what is datasource, why it is used and how can we configure it so that there are less chances of error from Content Editor while editing the same.
Thank you.. Keep Learning.. Keep Sitecoring.. 🙂