By Sitecore Coder to Sitecore Coders

Menu

Sitecore Experience Accelerator – What is new in rendering data source

I recently started looking into the features of Sitecore Experience Accelerator module and in this post I will share my understanding for few new ways of assigning datasource to a rendering that comes with SXA.

Motivation

With the new concept of page/partial design in SXA I noticed following things those motivated me to know more about it and write this blog post.

1. Relative Data Source:The first thing I noticed is the datasource field value as relative path in the rendering. As you can see in the below image, the selected item is a partial design item named as “Footer” and the control properties window shows the relative datasource for a control with some special starting keyword as “local” and after that a relative item path.

Figure 1: Page Relative Datasource

Clearly it means there must be some resolvers those will be doing the magic of converting a relative path to full item path.
The questions are which resolver is doing that? And are there any more resolvers?

2. Datasource change is not allowed from page for a rendering that belongs to partial design:
On a page if rendering is coming from partial design than from that page content authors cannot change the datasource for the item. The datasource can only be set on the partial design itself.

But lets assume there is a requirement to design a product detail page with using partial design. A rendering should be there in the partial design that must pick the content from product item field itself.
The question is does SXA has any solution?

2. Code Datasource:
A custom class can be created and type name of the class can be passed in the datasource. The custom class must implement the interface Sitecore.Buckets.FieldTypes.IDataSource. This interface contains single method named ListQuery. The method signature is as follows:

Item[] ListQuery(Sitecore.Data.Items.Item item)

As per the method signature, a Sitecore item will be retrieved as input and array of item will be returned. The resulting data source will be the IDs list of all items returned.

3. Page Relative Datasource
Instead of giving the full path of the item, a relative path can be passed that will be evaluated in respect of the Context Item, SXA will get the full path of the context item, appends the relative path mentioned in datasource at end and creates a full item path. This new full item path will be used as datasource value for the rendering. This resolver is used for the Relative Data Source item I mentioned in motivation section.

1. Page Relative Datasource
I have few items created under the home page and one of those is “About Us” page. I have placed the rendering on home page and used the relative page path in data source.

Figure 3: Page Relative Datasource assignment in presentation

Once published we can see the full item path on the page:

Figure 4: Page Relative Datasource result

2. Field Datasource
I have placed the rendering on the home page and used the field data source method and used the field name as “title”.

Figure 5: Field Datasource assignment in presentation

Once published we can see the title field value on the page:

Figure 6: Field Datasource result

There is no sample in this post with a link field but that is easy to do. You can add a link field to the page template and assign it a target Sitecore item. After that just go to the presentation details and change the field name to your link field name, publish all changes and open the page, you will see the item id of the item selected in link field.

3. Code DatasourceLet’s first decide what should the custom code must perform. For simplicity I want to build a custom code functionality that will treat all children item from the context item as datasource. Let’s add a custom data source class:

At the Sitecore end we have two items under the home item, let’s check their IDs first.

Figure 7: First child item under home

Figure 8: Another child item under home

Now let’s place the code source in the rendering data source:

Figure 9: Code Datasource assignment in presentation

Once published we can see the IDs of children pages on the home page:

Figure 10: Code Datasource result

Conclusion

Sitecore provides several ways to assign data source to a rendering. The existing data souring ways include providing full path of the Sitecore item, providing a Sitecore item ID or writing a query. SXA adds some new ways for data souring and gives more flexibility and power to the Sitecore architectures to design the solution.