Sitecore Experience Accelerator

Use a query to determine the data source context

Abstract

Learn how to use queries for rendering variants and data sources

When you create partial designs and snippets, it can be convenient to point renderings to a specific data source. For example, if you use a partial design for three different sites and you want the renderings of this partial design to use the context of the site. You can do this by changing the data source of the renderings manually, but you can also use queries.

For example, for a shared site you use a partial design named Header that contains an image rendering. If you use a query - query:$site/Data/Images/Logo - in the data source field of the image, then when this partial design is used by other sites, the Image rendering tries to resolve the image data source under the current site.

You can specify a query to run on a current context item by creating a rendering variant that contains the child item query: This enables you to query items and use SXA tokens such as $home and $site, for example, if you want the rendering variant to display only for the child items of a certain template. You must start your query with query and then use standard Sitecore query syntax.

The following table illustrates examples of query syntax:

Example

Query

Children of the current page

query:.//*

Home item of the current site

query:$home

Parent item of the current page

query:..

Parent of the parent item of the current page

query:../..

Every item under site home page

query:$home//*

Every item under site home page with additional sorting applied

query:$home//*[@@name='News']

All items of the Page template under the current item

query:..//*[@@templatename='Page']

All items of the Page template under the Home item of the current site

query:$site/*[@@name='Home']//*[@@templatename='Page']

This topic describes how to:

You can use queries to switch context by adding the query child item to a rendering variant or by adding a query to the data source of a rendering.

To add the query child item to a rendering variant:

  1. In the Content Editor, click the site and open the Presentation/Rendering Variants folder. This folder lists all the renderings that allow variants.

  2. Right-click the rendering that you want to add the variant to, and then click Insert, Variant Definition.

  3. Enter a name and the variant details.

  4. Right-click the variant, click Insert, and then click Query.

  5. In the Variant Details section, in the Query field enter the query.

    8F3406FDCB6045FCA0FF6600867034B6.png

To use a query in the data source field of a rendering:

  1. Click the rendering that you want to edit and in the floating toolbar, click Edit component properties.

  2. In the Control Properties dialog box, in the General section, in the DataSource field enter the query.

    12E475243CD243B8B12F20826B5FAE33.png