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.

askedJan 10, 2024 at 4:06
Sanjay Kumar's user avatar

2 Answers2

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));}
answeredJan 10, 2024 at 6:50
Sumit Salpekar's user avatar
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;CommentedJan 10, 2024 at 10:25
  • .Parameters are available inside "Sitecore.Mvc.Presentation", so you need to add this reference. @VineshCommentedJan 10, 2024 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 renderingif (rendering.RenderingItem != null && rendering.RenderingItem.InnerItem.TemplateName.Equals("Rendering Variant")){// Get the rendering variant ID from the rendering settingsvar 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    }}

}

answeredJan 12, 2024 at 10:38
Reginald Coghlan's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.