3

In SXA 1.8 I had a rendering variant with a VariantSection, with a style key on the Data attributes field, with value of background-image:url($(PromoImage));background-position: $(Horizontal Focal Point) $(Vertical Focal Point);

This was working totally fine, but when upgrading to SXA 10.1.0 I now get this error:

[ArgumentException: An item with the same key has already been added.]
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +60
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +14338200
Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderRenderingVariantFieldProcessor.GetAttributeTokenValues(Item item, T attributes, Action`3 collectionAction, MatchCollection matches, String value, String key) +326
Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderRenderingVariantFieldProcessor.GetAttributeTokenValues(Item item, IDictionary`2 attributes, MatchCollection matches, String value, String key) +219
Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderRenderingVariantFieldProcessor.GetVariantAttributes(RenderingVariantFieldBase variantField, Item item, Object model) +319
Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderRenderingVariantFieldProcessor.AddWrapperDataAttributes(RenderingVariantFieldBase variantField, RenderVariantFieldArgs args, HtmlGenericControl tag) +49
Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderSection.RenderField(RenderVariantFieldArgs args) +699
Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.RenderVariantField.RenderVariantFieldProcessor.Process(RenderVariantFieldArgs args) +138

If I change the field to only have one token in it, it works fine. It doesn't matter which token I keep. Why do multiple tokens now break it?

1

I have fixed this myself by overriding the following:

  • Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderSection
  • Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderTitle

With this code:

protected override void GetAttributeTokenValues(
        Item item,
        IDictionary<string, string> attributes,
        MatchCollection matches,
        string value,
        string key)
{
    //this.GetAttributeTokenValues(item, attributes, (collection, k, v) => collection.Add(k, v), matches, value, key);

    foreach (Match match in matches)
    {
        string attributeTokenValue = this.GetAttributeTokenValue(match.Groups[1].Value, item);
        value = value.Replace(match.Value, attributeTokenValue);
        if (attributes.ContainsKey(key))
        {
            attributes[key] = value;
        }
        else
        {
            attributes.Add(key, value);
        }
    }
}

I have raised this to sitecore support and they confirmed the issue with public ref 460090. As I have fixed this myself I didn't request a hotfix.

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.