1

I have a list of renderings used in an item. I used below code

Sitecore.Layouts.RenderingReference[] renderings = null;
renderings = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);
foreach (var rendering in renderings){

}

Is it possible to get which rendering variant is being used for all renderings? These all are SXA renderings.

2 Answers 2

1

You can access the rendering variant through rendering parameters. As it's a drop-down, it provides you with the GUID i.e the Sitecore ID of the rendering variant. Subsequently, you can retrieve its details through the GetItem method. Please refer the below code,

Sitecore.Layouts.RenderingReference[] renderings = null;
renderings = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);
foreach (var rendering in renderings){
 var parms = rendering.Parameters;
 renderingVariantItemID= parms["Variant"]; 
 Item renderingVariantItem = rSitecore.Context.Database.GetItem(new ID(renderingVariantItemID));
}
2
  • Do I need to add any reference it's showing an error that renderingReferance does not contain a definition of the parameter? i am currently using using Sitecore.Data.Fields; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;
    – Vinesh
    Jan 10 at 10:25
  • .Parameters are available inside "Sitecore.Mvc.Presentation", so you need to add this reference. @Vinesh Jan 10 at 10:58
0
Sitecore.Layouts.RenderingReference[] renderings = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);

foreach (var rendering in renderings) { // Check if the rendering is an SXA rendering if (rendering.RenderingItem != null && rendering.RenderingItem.InnerItem.TemplateName.Equals("Rendering Variant")) { // Get the rendering variant ID from the rendering settings var variantId = rendering.Settings["Variation ID"];

    // Get the rendering variant item based on the ID
    var variantItem = Sitecore.Context.Database.GetItem(variantId);

    // You can now access properties of the rendering variant item, such as its name
    if (variantItem != null)
    {
        var variantName = variantItem["Name"];
        // Do something with the rendering variant information
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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