Experience Sitecore ! | January 2019

Experience Sitecore !

More than 200 articles about the best DXP by Martin Miles

I have been awarded the Sitecore Most Valuable Professional Technology Award in 2019 - Sitecore MVP

Just got an email with absolutely positive news. It stated:


Great news, Sitecore! 

That is always a pleasure to realise that you are helpful to others, and your efforts were spent for good. 

In my opinion, the two most interesting bonus of being an MVP are early preview of new Sitecore platform and its parts and taking part in annual MVP Summit, taking place along with Symposium, where you can actually talk to all these people who build the platform and get their internal vision, discuss features and roadmap, give feedback and report some bugs (if any). Ah, they also sent me this badge, so that I can append it to blog or some other projects:

Last, but not the least benefit, is to be a part of such an enthusiastic community, among the best and smartest, among these people (see the list of the winners grouped by countries):

This is my third MVP award in a row, however, will keep working hard, hopefully, get one more next year. 

Creating a custom rendering variant section to render element with background image

Note! The code used in this post can be cloned from GitHib repository: SXA.Foundation.Variants

I am working on a rendering variant, and was looking on getting an element with background image from Sitecore, something similar to this:

<div style="background-image: url(/-/media/Project/Tenant/Platform/Some-image-from-Sitecore.jpg)">
    <div>... some rendering variant fields here</div>
There is way to achieve that by placing container rendering, as it has an option of picking up a background image:

However, even it may bring desired result, having container it is not the best and simplest from structuring point of view as it creates plenty of nesting element and image comes not at the top of them, not to say that it is not editors friendly and have to mess with placeholder settings. ideally, I wanted something like Section which in fact is just a div container to store other variant fields, but with style attribute stating background image. Okay, since I found nothing, I can implement that with a new variant field.

Since Section seems to be the best option, let's just clone and extend it. To start with, I copy VariantSection template into my tenant templates folder under foundation, and name it Image Section. I add just one ImageFieldName field of type image to the newly created template, that will be a reference to background image. So far, so good.

Then I run /sitecore/admin/showconfig.aspx in order to investigate what logic is involved into processing sections and find these 2 processors, one for parser:

<parseVariantFields patch:source="Sitecore.XA.Foundation.RenderingVariants.config">
  <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseSection, Sitecore.XA.Foundation.RenderingVariants" resolve="true"/>
and another - for renderer:
<renderVariantField patch:source="Sitecore.XA.Foundation.RenderingVariants.config">
  <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderSection, Sitecore.XA.Foundation.RenderingVariants" resolve="true"/>

With JetBrains dotPeek, it becomes fairly easy to find the code and extend it. Based on what I found, I create 3 new classes to implement parser, renderer and also the model itself:

I extend section variant field with one extra Single-Line Text field, stating which field from the context item should contain image. Also, I am referencing my new template created earlier. Here's the code:

using System.Collections.Generic;
using Sitecore.Data.Items;
using Sitecore.XA.Foundation.Variants.Abstractions.Fields;
using Sitecore.XA.Foundation.RenderingVariants.Fields;

namespace Platform.Foundation.Variants.Pipelines.VariantFields
    public class VariantImageSection : RenderingVariantFieldBase
        public VariantImageSection(Item variantItem) : base(variantItem)

        public static string DisplayName => "Image Section";

        public IEnumerable<BaseVariantField> SectionFields { get; set; }

        public string ImageFieldName { get; set; }

        public string LinkField { get; set; }

        public bool IsLink { get; set; }

and now implement parser. Please pay attention that since we duplicated the template, fields for Image section will have new IDs, so we will also re-reference Tag, CssClass and IsLink fields from our static constants class, where we keep all the IDs:

using System.Collections.Generic;
using Sitecore.Data;
using Sitecore.DependencyInjection;
using Sitecore.XA.Foundation.SitecoreExtensions.Extensions;
using Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.ParseVariantFields;
using Sitecore.XA.Foundation.Variants.Abstractions.Services;

namespace Platform.Foundation.Variants.Pipelines.VariantFields
    public class ParseImageSection : ParseVariantFieldProcessor
        public override ID SupportedTemplateId => Constants.RenderingVariants.Fields.VariantImageSection;

        public override void TranslateField(ParseVariantFieldArgs args)
            ParseVariantFieldArgs variantFieldArgs = args;

            var variantSection = new VariantImageSection(args.VariantItem);
            variantSection.ItemName = args.VariantItem.Name;
            variantSection.Tag = args.VariantItem.Fields[Constants.RenderingVariants.Fields.Tag].GetEnumValue();
            variantSection.CssClass = args.VariantItem[Constants.RenderingVariants.Fields.CssClass];
            variantSection.ImageFieldName = args.VariantItem[Constants.RenderingVariants.Fields.ImageField];
            variantSection.LinkField = args.VariantRootItem[Sitecore.XA.Foundation.Variants.Abstractions.Templates.IVariantDefinition.Fields.LinkField];
            variantSection.IsLink = args.VariantItem[Constants.RenderingVariants.Fields.IsLink] == "1";

            variantSection.SectionFields = args.VariantItem.Children.Count > 0 
                ? ((IVariantFieldParser)ServiceLocator.ServiceProvider.GetService(typeof(IVariantFieldParser))).ParseVariantFields(args.VariantItem, args.VariantRootItem, false) 
                : new List<Sitecore.XA.Foundation.Variants.Abstractions.Fields.BaseVariantField>();

            variantFieldArgs.TranslatedField = variantSection;

Now we implement renderer logic. What I am doing below, is getting a field name of a context item, where image supposed to be, and then generate a URL for that media item (in case field is valid and media item indeed presents there), further down this URL is being appended our section element (remember, you may select any tag, not just div) as a background image style attribute:

using System;
using System.Web.UI.HtmlControls;
using Sitecore.Data.Fields;
using Sitecore.Pipelines;
using Sitecore.Resources.Media;
using Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField;
using Sitecore.XA.Foundation.Variants.Abstractions.Fields;
using Sitecore.XA.Foundation.Variants.Abstractions.Models;
using Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.RenderVariantField;

namespace Platform.Foundation.Variants.Pipelines.VariantFields
    public class RenderImageSection : RenderRenderingVariantFieldProcessor
        public override Type SupportedType => typeof(VariantImageSection);

        public override RendererMode RendererMode => RendererMode.Html;

        public override void RenderField(RenderVariantFieldArgs args)
            string styleInlineValue = String.Empty;
            var variantField = args.VariantField as VariantImageSection;

            if (!string.IsNullOrWhiteSpace(variantField?.ImageFieldName) && args.Item != null)
                ImageField imgField = args.Item.Fields[variantField.ImageFieldName];
                if (imgField != null)
                    string url = MediaManager.GetMediaUrl(imgField.MediaItem);
                    styleInlineValue = $"background-image: url({url})";

            var tag = new HtmlGenericControl(string.IsNullOrWhiteSpace(variantField.Tag) ? "div" : variantField.Tag);

            if (styleInlineValue.Length > 0)
                tag.Attributes.Add("style", styleInlineValue);

            AddClass(tag, variantField.CssClass);
            AddWrapperDataAttributes(variantField, args, tag);

            foreach (BaseVariantField sectionField in variantField.SectionFields)
                var variantFieldArgs = new RenderVariantFieldArgs
                    VariantField = sectionField,
                    Item = args.Item,
                    HtmlHelper = args.HtmlHelper,
                    IsControlEditable = args.IsControlEditable,
                    IsFromComposite = args.IsFromComposite,
                    RendererMode = args.RendererMode,
                    Model = args.Model

                CorePipeline.Run("renderVariantField", variantFieldArgs);

                if (variantFieldArgs.ResultControl != null)

            args.ResultControl = variantField.IsLink ? InsertHyperLink(tag, args.Item, variantField.LinkAttributes, variantField.LinkField, false, args.HrefOverrideFunc) : tag;
            args.Result = RenderControl(args.ResultControl);

Once done, create a patch configuration file and reference new processors: parser and renderer:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <processor type="Platform.Foundation.Variants.Pipelines.VariantFields.ParseImageSection, Platform.Foundation.Variants" resolve="true"/>
        <processor type="Platform.Foundation.Variants.Pipelines.VariantFields.RenderImageSection, Platform.Foundation.Variants" resolve="true"/>

The code above requires you to reference Sitecore.XA.Foundation.RenderingVariants, Sitecore.XA.Foundation.SitecoreExtensions and Sitecore.XA.Foundation.Variants.Abstractions libraries.

Another thing you will need to provide is adding insert options. I do not cover it as it is quite obvious. Finally, you'll be able to use your new rendering variant field:


which renders the following output and allows child / nested elements, as normal section would do:

This explains how one can easily extend existing rendering variant fields and provide any custom output without lengthy process of creating a custom component in SXA.

Rule missing OOB in Sitecore, where the Experience Editor is in editing mode

I was playing with SXA and wanted to have a custom element within a rendering variant that identifies current control presents on a page, otherwise it is being invisible. Looking for such a rule I suddenly realised it is not there, so had to create my custom one. I am sharing it just in case it might be useful to someone looking for a similar solution.

So, as per Rules Engine Cookbook, you create a tag, custom condition and relevant element for it:

The code is simple.

public class IsExperienceEditorEditing<T> : StringOperatorCondition<T> where T : RuleContext
    public int Value { get; set; }
    protected override bool Execute(T ruleContext)
        return Sitecore.Context.PageMode.IsExperienceEditorEditing;

Also, working with SXA aligned with Helix principles, it would make sense to create a foundation module for custom rules, drop the above class inside as well as serialization for newly created tag and elements:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:unicorn="http://www.sitecore.net/xmlconfig/unicorn/">
  <sitecore unicorn:require="On">
        <configuration name="Foundation.Rules" description="Foundation Rules" extends="Helix.Foundation">
          <predicate type="Unicorn.Predicates.SerializationPresetPredicate, Unicorn" singleInstance="true">
            <include name="$(layer).$(module).Tags" database="master" path="/sitecore/system/Settings/Rules/Definitions/Tags/Experience Editor" />
            <include name="$(layer).$(module).Elements" database="master" path="/sitecore/system/Settings/Rules/Definitions/Elements/Experience Editor" />
So now I can have an element withing rendering variant that is shown only when in editing mode:

Hope you find this helpful!

Quick tip: using rules engine for SXA rendering variants

I working on a rendering variant in order to implement a component similar to the one below:

The component should display a heading title, followed by a list of promo blocks leading to the other pages. What is important here, is that different page types can be assigned into given component, of course they all do implement _Promotable interface template that has fields allowing them being promoted through this component. So far, so good.

But notice, each promo block has its type in left top corner painted into appropriate color. The values of it (article, topic, blog) are actually types of the pages, they do not present in generic _Promotable interface template, but do match template names, so why not to expose template names for these fields?

The easiest way of doing that is to create a label for each of the page type and assign it corresponding CSS class to display in appropriate color. 

Then we may use built in Rules Engine in order to create a rules for each of these page type labels to be shown only if the item template matches given page type. Here's how it looks in Content Editor:

Note! If you cannot access your condition through SXA built-in Rule Engine, you need to assign tag to Conditional Renderings tags (/sitecore/system/Settings/Rules/Conditional Renderings):

Also, there is another way of achieving the same goat - using NVelocity templates: create a Variant Template field and expose current template name:

That will also work since type badge matches name of page template in our case, but will need some extra work to wire up CSS class, that should derive from template name in this or that way.

SXA built-in token tools to be used with rendering variant templates

Note! The code used in this post can be cloned from GitHib repository: SXA.Foundation.Variants

You might came across Variant Template Field when going through rendering variants insert options and wondered, what is that? Variant Template Field is a special field that uses NVelocity templates (supported by Sitecore.NVelocity.dll) in order to render the value.

One of the MVPs - Michael West - has briefly mentioned built-in token tools coming with SXA. I decided to take a look on these tools. I found them in Sitecore.XA.Foundation.Variants.Abstractions.dll under Sitecore.XA.Foundation.Variants.Abstractions.NVelocityExtensions namespace. Here's their code:

public class DateTool
    public string Format(DateTime dateTime, string format) => dateTime.ToString(format);

    public string Format(string dateTimeString, string format)
        DateTime time = DateUtil.IsoDateToDateTime(dateTimeString, DateTime.MinValue);
        if (time != DateTime.MinValue)
            return time.ToString(format);
        return dateTimeString;


public sealed class NumberTool
    public string Format(double value, string format) => value.ToString(format, CultureInfo.InvariantCulture);
    public string Format(int value, string format) => value.ToString(format, CultureInfo.InvariantCulture);
    public string Format(float value, string format) => value.ToString(format, CultureInfo.InvariantCulture);

DateTool is used internally by another field type - VariantDateField, that's how it works. Let's say I have a template:

and it renders to: 

But you may also use it from templates (since it is already mapped) for example combining with your custom tools (not just this one line):

resulting with:

It is also briefly mentioned in the official documentation.

Configuring search for SXA 1.8

I was following the official guidance, so dropped search box component to my header and configured search results parameter. Then created page /search and dropped search results on it. Tested and got an exception (from logs):

14260 17:31:37 ERROR Unable to connect to [https://localhost:8983/solr], Core: [sitecore_sxa_master_index]
Exception: SolrNet.Exceptions.SolrConnectionException
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Error 404 Not Found</title>
<h2>HTTP ERROR 404</h2>
<p>Problem accessing /solr/sitecore_sxa_master_index/schema. Reason:
</p><pre>Not Found</pre><p></p>

Source: SolrNet
   at SolrNet.Impl.SolrConnection.Get(String relativeUrl, IEnumerable`1 parameters)
   at Sitecore.ContentSearch.SolrNetExtension.Impl.SolrBasicServerEx`1.GetSchema(String collection)
   at Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.InitializeSchema()
   at Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.InitializeSolr()

Nested Exception
Exception: System.Net.WebException
Message: The remote server returned an error: (404) Not Found.
Source: System
   at System.Net.HttpWebRequest.GetResponse()
   at HttpWebAdapters.Adapters.HttpWebRequestAdapter.GetResponse()
   at SolrNet.Impl.SolrConnection.GetResponse(IHttpWebRequest request)
   at SolrNet.Impl.SolrConnection.Get(String relativeUrl, IEnumerable`1 parameters)
14260 17:31:37 ERROR Unable to connect to [https://localhost:8983/solr], Core: [sitecore_sxa_web_index]

Exception: SolrNet.Exceptions.SolrConnectionException

Obviously, my SXA indexes got misconfigured, and Sitecore Indexing manager showed nothing there. I identified a responsible config file: App_Config/Modules/SXA/Z.Foundation.Overrides/Sitecore.XA.Foundation.Search.Solr.config - it adds two SXA-related indexes as below (irrelevant lines skipped):

<index id="sitecore_sxa_master_index">
  <param desc="name">$(id)</param>
  <param desc="core">$(id)</param>

I tried populating schema:

 But got such an exception:

Looking at Solr I've noticed that all my cores are prefixed (ie. Platform_master_index) by the installation, so obviously there are no such cores (ie. sitecore_sxa_master_index). Indexing Manager claims these as "never run". I decided to collocate SXA indexes within cores for corresponding databases, along with traditional non-SXA indexes.  So that both indexes sitecore_master_index and sitecore_sxa_mater_index will reside within Platform_master_index core.

So I ended up making this patch Foundation.ContentSearch.config:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
  <sitecore search:require="solr">
      <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
        <indexes hint="list:AddIndex">
          <index id="sitecore_sxa_master_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider" role:require="Standalone or Reporting or ContentManagement or Processing">
            <param desc="core" patch:instead="param[@desc='core']">Platform_master_index</param>
          <index id="sitecore_sxa_web_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider" role:require="Standalone or ContentDelivery or ContentManagement or Reporting">
            <param desc="core" patch:instead="param[@desc='core']">Platform_web_index</param>

Once patch applied, I've re-populated schema again and it worked and after refreshing the page search box worked well, including predictive search!

Which certificates (and where to) got installed with Sitecore 9.1?

Upon the new clean installation, Sitecore 9.1 puts the following certificates (as per below example of habitat project hostnames):

1. Current User\Personal - nothing

2. Current User\Intermediate Certification Authorities - SIF

  • Sitecore Install Framework / Sitecore Install Framework

3. Local Computer\Personal - xConnect and Identity Server

  • habitat_xconnect.dev.local / DO_NOT_TRUST_SitecoreRootCert
  • habitat_IdentityServer.dev.local / DO_NOT_TRUST_SitecoreRootCert

4. Local Computer\Intermediate Certification Authorities - Sitecore and SIF

  • DO_NOT_TRUST_SitecoreRootCert / DO_NOT_TRUST_SitecoreRootCert
  • Sitecore Install Framework / Sitecore Install Framework

Hope this helps!