1

I want to read a site setting property (defined in Setting/Site grouping/'Sitename') in a SXA rendering variant template. Is this feasible in sitecore 9.2?

1
  • Just don't do it that way. This is your sign to clone the component and add your own controller/repository and view. Doing this in a repository is simple and far safer than trying to add the logic to a rendering variant. – Richard Seal Oct 5 '20 at 14:04
1

I don't think it is possible ootb.

One (bad) option would be to write something in NVelocity - but as that is not future proof I really would not do that.

In 9.3 the best solution would be to write a simple extension on Scriban but as you are on 9.2 that is not yet possible.

A last option I see would be to write a custom variant. I've got an example of how to do that here: https://ggullentops.blogspot.com/2019/04/sitecore-sxa-custom-rendering-variant-translation.html. The blog explain a setup for translations, but if you change the logic in the render processor to read from the site settings that would be a good solution.

In short, how to create a custom variant definition:

  • (sc) Create a template for the variant (you could inherit from VariantText)
  • (sc) Create insert options so you can actually add items from that template within variants (use the rules engine - /sitecore/system/Settings/Rules/Insert Options/Rules)
  • (code) Create a model (you could inherit from VariantText again)
  • (code) Create a ParseVariantFieldProcessor that creates your model from the template
  • (code) Create a RenderRenderingVariantFieldProcessor that actually renders the output (this is where the logic will be to get a site setting - based on the input in the variant template)
  • (config) inject those processors in the correct pipelines
  • (sc) Create your variant

This might seem like a lot of work, but it's not that hard so if you really need it that is a possible way to go.

ps: you can get the site settings just like in a non-SXA site (use the Properties of the "Site"). If for some reason you would need the actual item that represents the site you can use multisiteContext.SiteItem)

0

I tried to get the Site Grouping item like this. Hope this will help you.

using Sitecore.XA.Foundation.Multisite;

public class MediaHelper
{
    protected ISiteInfoResolver _siteInfo { get; set; }

    public MediaHelper(ISiteInfoResolver siteInfo)
    {
        this._siteInfo = siteInfo;
    }

    private Item GetRootItem(Item item)
    {
        var siteInfo = this._siteInfo.GetSiteInfo(item);

        if (siteInfo != null)
        {
            var homeItemPath = string.Concat(siteInfo.RootPath, siteInfo.StartItem);

            return homeItemPath.GetItemByPath(item.Database);
        }
        return null;
    }
}

The properties RootPath and StartItem give you the values. Also, you will find more properties that will help you to get the field that you need.

Finally, if you want to read the key-value pair like other properties,

enter image description here

then you can use this line of code.

var dictionaryPath = siteInfo.Properties["dictionaryPath"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.