Datasources in Sitecore

Datasources are used for various reasons and at various places. There are mainly 2 types of Data sources.

Field Datasources
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.
SRG276

These types of Datasources are known as Field Datasources and are widely used.

Rendering Datasources
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.

  1. Go to Visual Studio.
  2. Open ControllerRendering controller.
  3. Modify the code to fetch the list from datasource and not directly from the item.
    SRG277
  4. Comment the previous logic. And add the new logic.
  5. Publish the Solution since there is code behind changes.
  6. Go to Sitecore and Open Content Editor.
  7. Navigate to the Test Item.
  8. Click on Presentation tab >> Details.
    SRG278
  9. Click on Final Rendering >> Controller rendering.
    SRG279
  10. Click Browse under Datasource field and select the Fruits folder.
    SRG280
  11. Click OK and publish the Test Item.
  12. Browse http://learningsitecore/test-item
    SRg281
  13. 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.

  1. Go to Sitecore >> Content Editor >> Test Item >> Launch Experience Editor.
    SRG282
  2. Go to View >> Check Designing checkbox. Click on Controller Rendering part.
  3. Click on the drop down arrow on the 5th option >> Change associated content.
    SRg283
  4. By default fruits folder will be selected as it is our current datasource. Select Color folder and click OK.
    SRG284
  5. And the content is changed.
    SRG285
  6. 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.

  1. Go to Content Editor and Navigate to Controller rendering (/sitecore/layout/Renderings/Controller rendering)
  2. Scroll down To Editor Options section and find the field ‘Datasource Template’.
  3. Click Insert link.
    SRG286
  4. Select Folder template as we select folders as our datasources in our examples.
    SRG287
  5. Click Insert. Save the changes.
  6. Switch to Experience Editor and Reload it.
  7. Click on Controller rendering area >> Click dropdown arrow next to the 5th option >> Change associated content.
  8. By default Color folder is selected as it is our datasource. Click on any other item which is not a folder.
    SRG288
  9. 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.
  10. 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.. 🙂

10 thoughts on “Datasources in Sitecore

  1. Pingback: How to set multiple datasource in Datasource Location field | Sitecore Diaries

  2. I simply want to tell you that I am beginner to blogging and site-building and absolutely enjoyed you’re web site. Probably I’m want to bookmark your site . You actually come with perfect article content. Thanks a bunch for sharing with us your web-site.

    Like

  3. Pingback: Sitecore Datasource – Developer Blog

  4. view part is missing in that , you are using controller rending , all things are good not not mention view here can you please add the misisng part view to make easy for learner

    Like

Leave a comment