Experience Sitecore ! | All posts tagged 'Query-string'

Experience Sitecore !

More than 200 articles about the best DXP by Martin Miles

What is Query Variant Field to be used with rendering variants and few real life scenarios of using it

Imagine a case - there is a datasource item, that has a Droplink field to pointing to another item, let's say a Rich Text with an address (as on an image below):


That means, in your rendering variant you simply use Reference Variant Field referencing an item behind Rich text droplink, and within that Rich text item you simply render a field, as normal:


Seems obvious, right? Let's increase complexity!


Scenario one

We change scenario so that Droplink would now point not to an item having the field to render, but rather to an item, containing other children you might need to display:


So instead of referencing a Rich text item, we'll reference Link list item, where you need to iterate child items (links) of a referenced Link List item, and show all these links on a page with a rendering variant:


Question: how do I iterate children of my referenced item?

Okay, that's where your new friend comes to help you. Welcome, Query Rendering Variant Field!

What it actually does is also switching a context to a single item / multi items, similarly to Reference Variant Field, but instead of taking switching context from a link-powered field, it uses Sitecore search query and switches context to its execution result. Here's how one can add Query Variant Field into a rendering variant:


So for my simple example with Link List children, I use a query to search all the children of a current item (Link List):


That does the job and withing this query item you may use fields related to items coming out from a search.

One more helpful tip: when working with references, queries or whatsoever changes the context, you would often wondering iа changing context worked well and referencing what you intend to. The trick I use is inserting a debugging template field exposing ID and/or a name of where we've switched to (or any other universal property all the items do have and that are accessible from NVelocity $item):


This takes only 5 seconds of my time to implement but immediately helps to visualise a context on output. Very helpful!


Second scenario

... where Query Field Variant helps me so much. I need to implement a Subscription Level identifier on every page, so that it shows which privilege a current user has (obviously, he/she will see an only single one of the labels show below at one time):


But there is a requirement for that labels itself to be changeable and kept somewhere under site's settings item - subject to alterations at a later stage. So I already have a separate folder with such items:


With using a Reference Variant Field one would need to have rendering having a datasource item with a field pointing to one of those subscription items, which is not an elegant scenario. And what if my rendering does not accept datasources? Or it simply breaks sense, as in a described scenario?

Since we know the IDs of these subscription items we can reference them directly and that's where Query Variant Template also helps us. Also, search query by an ID always returns not more than one item, and since we know item exists we can use it as direct linking. That's how I do that:


Works like a charm!

I have just shown only two real-life examples of using Query Variant Field, but the potential area of their usage is virtually unlimited - as much as you can query you indexes. This gives an ultimate tool for comfy switching context as much as you may need that, but I'd warn you from overusing it too much.

SXA tokens for datasource queries

Just a cheat sheet for myself to have them here all in one place so that I bookmark this URL and don't waste time digging out all sorts of documentation in order to find the right one. Taken out from here and here.

Available tokens:

  • $compatibleThemes - path to all themes
  • $theme - currently used theme
  • $pageDesigns - root of page designs
  • $partialDesigns - root of partial designs
  • $currenttemplate - name of the current template
  • $tenant - path to the current tenant
  • $site - path to the current site
  • $home - path to the current site start item (Home)
  • $templates - path to the current sitetemplates
  • $siteMedia - path to Virtual Media folder located under site
  • $sharedSites - for multiroot fields, resovles shared site for current tenant.
Custom tokens can be defined by adding an additional processor to resolveTokens pipeline.

Some samples
Children of the current pagequery:.//*
Home item of the current sitequery:$home
Parent item of the current pagequery:..
Parent of the parent item of the current pagequery:../..
Every item under site home pagequery:$home//*
Every item under site home page with additional sorting appliedquery:$home//*[@@name='News']
All items of the Page template under the current itemquery:..//*[@@templatename='Page']
All items of the Page template under the Home item of the current site       query:$site/*[@@name='Home']//*[@@templatename='Page']

Other examples met in projects per field types

Droptree
query:$site
query:$site/Data/Authors
query:$site/*[@@templatename='Presentation']/*[@@templatename='Partial Designs']
Datasource=/sitecore/system/Marketing Control Panel/Outcomes&IncludeTemplatesForSelection=outcome definition&AllowMultipleSelection=no
DataSource=/sitecore/system/Settings/Security/Profiles/Habitat/UserProfile&DatabaseName=core

Droplink
query:$site
code:Sitecore.XA.Foundation.Variants.Abstractions.DataSource.AvailableRenderingVariants, Sitecore.XA.Foundation.Variants.Abstractions
query:$compatibleThemes
query:$site/Settings/Scopes//*[@@templateid='{8B649372-CC12-4F31-802A-8C3B3D09BB3F}']|$sharedSites/Settings/Scopes//*[@@templateid='{8B649372-CC12-4F31-802A-8C3B3D09BB3F}']
query:$pageDesigns//*[@@templatename='Page Design']
query:/sitecore/system/Settings/Foundation/Experience Accelerator/Rendering Variants/Rendering Variants/*[@@templatename='Variants Grouping']/POI//*[@@templatename='Variant Definition']|$site/*[@@name='Presentation']/*[@@templatename='Variants Grouping']/POI//*[@@templatename='Variant Definition']|$sharedSites/*[@@name='Presentation']/*[@@templatename='Variants Grouping']/POI//*[@@templatename='Variant Definition']
query:$site/*[@@name='Presentation']/*[@@templatename='PoiTypes']//*[@@templatename='POI Type']|$sharedSites/*[@@name='Presentation']/*[@@templatename='PoiTypes']//*[@@templatename='POI Type']

Droplist
databasename=core&datasource=/sitecore/content/Applications/WebEdit/Edit Frame Buttons        (edit frame select buttons)

Multilist
query:/sitecore/system/Marketing Control Panel/Profiles//*[@@templatename='Profile']
query:/sitecore/system/Marketing Control Panel/Profiles//*[@@templatename='Profile']
query:/sitecore/system/Marketing Control Panel/Profiles//*[@@templatename='Profile']
query:$templates

Treelist
datasource=/sitecore/media library&IncludeTemplatesForDisplay=Media Folder,Base Theme,Theme
query:$site/*[@@name='Data']/*[@@templatename='Tag Folder']
DataSource=/sitecore/media library&IncludeTemplatesForSelection=Theme
DataSource=query:$site/Data/Links
DataSource=/sitecore/system/Settings&IncludeTemplatesForSelection=EditTenantTemplate,AddItem,ExecuteScript
DataSource=query:$tenant&IncludeTemplatesForSelection=Site
Datasource=/sitecore/media library&ExcludeTemplatesForSelection=Main section
datasource=/sitecore/layout/renderings&excludetemplatesforselection=Folder,Rendering Folder,Node
query:$tenant

TreelistEx
DataSource=/sitecore/layout&ExcludeItemsForDisplay={E18F4BC6-46A2-4842-898B-B6613733F06F},{75CC5CE4-8979-4008-9D3C-806477D57619},{1CE3B36C-9B0C-4EB5-A996-BFCB4EAA5287},{B4A0FB13-9758-427C-A7EB-1A406C045192}&ExcludeTemplatesForSelection={239F9CF4-E5A0-44E0-B342-0F32CD4C6D8B},{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}
DataSource=/sitecore/layout&ExcludeItemsForDisplay={E18F4BC6-46A2-4842-898B-B6613733F06F},{75CC5CE4-8979-4008-9D3C-806477D57619},{1CE3B36C-9B0C-4EB5-A996-BFCB4EAA5287},{B4A0FB13-9758-427C-A7EB-1A406C045192},{75D27C2B-5F88-4CC8-B1DE-8412A1628408},{B87CD5F0-4E72-429D-90A3-B285F1D038CA},{39522E9A-9B6E-4CB4-850C-23D0D3ADFD0C}&ExcludeTemplatesForSelection={239F9CF4-E5A0-44E0-B342-0F32CD4C6D8B},{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}
IncludeTemplatesForSelection=Site

Multiroot Treelist
DataSource=query:$partialDesigns&IncludeTemplatesForSelection=Partial Design,Metadata Partial Design&IncludeTemplatesForDisplay=Partial Design,Metadata Partial Design,Partial Designs,Partial Design Folder

Image
query:$siteMedia

General Link
query:$home

Rich text
/sitecore/system/Settings/Html Editor Profiles/Rich Text XA

Rules
rulespath=/sitecore/system/Settings/Foundation/Experience Accelerator/Variants/Rules Context&hideactions=true&allowmultiple=true
rulespath=/sitecore/system/Settings/Foundation/Experience Accelerator/Search/Item Query Rules Context&hideactions=true&allowmultiple=true

Lookup name lookup value list
query:/sitecore/layout/Devices//*[@@templatename='Device']||query:$compatibleThemes
query:/sitecore/system/Settings/Foundation/#Experience Accelerator#/Theming/Enums/Placeholders/*||query:/sitecore/templates/Foundation/#Experience Accelerator#/Grid/#Grid Definition#/#Placeholder Styles#/*
query:$templates||query:$pageDesigns//*[@@templatename='Page Design']
query:/sitecore/layout/Devices//*[@@templatename='Device']||query:/sitecore/system/Settings/*//*[@@templatename='Grid Definition']

Tag treelist
query:$site/*[@@name='Data']/*[@@templatename='Tag Folder']

Styling parameters

code:Sitecore.XA.Foundation.Presentation.CodeDataSource.RenderingStyles, Sitecore.XA.Foundation.Presentation


Sitecore Personalization based on URL query string parameters

Once I was asked to personalize Sitecore component depending on custom URL query string parameter, to identify users coming by a promo campaign in order to display them slightly modified component reflecting campaign presentation. Easy, I thought, but in next couple minutes struggled to find that condition in Rules Engine. It is not in Sitecore, what a surprise!..

So, let's go through and see what conditions are in Sitecore and how you can create any custom condition you would ever imagine.


First of all, all stuff for Rules Engine is specified as Sitecore items underneath /sitecore/system/Settings/Rules folder. So let's create an item named Query String Value Presents of /sitecore/templates/System/Rules/Condition template within /sitecore/system/Settings/Rules/Conditional Renderings/Conditions/URL Query String Conditions folder. There are just two important fields we are going to set. Type field, as it is very common to Sitecore, specifies fully qualified class and assembly names, where business logic is implemented. Another, Text field, is more interesting on that stage - it shows wordings that would be presented to user when using his condition with Rules Engine. Here is what we set there:

Where the User [QueryStringName,,,QueryString Name] has a value that [operatorid,StringOperator,,compares to] [QueryStringValue,,,QueryString Value].

Pay attention to parameters in square brackets - they would be replaced by Rules Enging to selectors.

Now let's look at the code. Fom the item we ahave referenced the class.

public class QueryStringCondition<T> : StringOperatorCondition<T> where T : RuleContext

All derived condition classes should have same definition and derive from base StringOperatorCondition class. As the absolute minimal, we are to override just one Execute(T ruleContext) method. Additionally we must create public string properties named exactly the same as in parameters above from Text field from condition definition item in Sitecore.

public string QueryStringName { get; set; }
public string QueryStringValue { get; set; }
protected override bool Execute(T ruleContext)
{
    // process QueryStringName and QueryStringValue properties here
    // return true if personlization parameters falls within the condition 
}

QueryStringName and QueryStringValue would be auto-populated by Rules Engine.

With our next example we are trying to display an additional promo component when user access our website by URL with sourceId=campaign as parameters.


Full implementation of QueryStringCondition<T> class can be found on GitHub by this link.

Hope you find this helpful!