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 Answers2
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));}- 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;Sanjay Kumar– Sanjay Kumar2024-01-10 10:25:10 +00:00CommentedJan 10, 2024 at 10:25
- .Parameters are available inside "Sitecore.Mvc.Presentation", so you need to add this reference. @VineshSumit Salpekar– Sumit Salpekar2024-01-10 10:58:42 +00:00CommentedJan 10, 2024 at 10:58
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 }}}
Explore related questions
See similar questions with these tags.