Sitecore tip 29: the SXA use page as datasource option for components

Did you know there is an option to configure for certain SXA components that they can use the page they are placed on as a datasource? So what can you do with it?

By default there is one component that can do this: the Page Content component. You can create rendering variants with field names that relate to the page fields. So for example for a news article you can create a fixed layout with the Page Content component by adding fields for an abstract, publishdate, author and whatever you need.

But this week’s tip was not about the Page Content component. So let’s talk about another one. For example, my old time favourite, the Promo component. Let’s say you have a specific designed intro block for a landingpage. It contains a background image and on top of it, there is a title, a short introduction text of the page and a call to action button. You want to store this information on page level in stead of creating seperate datasource items for a promo component and store them below the page.

This is where the use page as datasource option comes in. Here are the steps you can take:

1. Add the desired fields to your page data template.

2. Create a rendering variant for the promo component and add rendering variant fields that relate to the fields of your page data template.

3. Go to your /sitecore/content/Your Tenant/Your Site/Settings/Datasource Configurations and add a new configuration for the Promo component.

4. With the newly created datasource configuration, scroll down to the Experience Accelerator section and check the option Can select Page as a data source.

5. Now, create or go to a page of the correct data template, fill in the fields and add the Promo component. Your newly created intro block should automatically appear.

Things to know

This is cool right? It is another way of speading up the work of your content editors besides options like snippets. But with this feature there are a few things to be aware of:

  • Unfortunately you cannot choose different rendering variants when the component is added to the page. So there can only be one which the component automatically selects.
  • When you create multiple rendering variants for your component that uses the page as a datasource, the component will pick the first rendering variant that has the appropriate fields. So if you create 2 versions only the first one will be used.
  • In this example I used the Promo but if you want to do it right, you should clone the Promo component and create a new one called “Landingpage intro” or something like that.

Like I said before, there are other ways to achieve the result I used for this week’s tip, but I hope it explains what the feature does. How you end up using it is entirely up to you.