2

I am using Sitecore 10.3 with SXA, I am trying to read all the renderings of the current item and get datasource of a required rendering item based on the item ID, But I am able to see only which are present in final renderings but not able see the renderings from page design.

How can I access the page design renderings.

I tried:

renderingItem.Visualization.GetRenderings(Sitecore.Context.Device, true);
1

1 Answer 1

3

You can use below code snippet to get all the rendering on an item including renderings from page/partial design.

using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Mvc.Pipelines.Response.GetXmlBasedLayoutDefinition;
using Sitecore.Pipelines;
using System.Web.Mvc;
using System.Xml;
using System.Xml.XPath;

public static class SitecoreHelperExtensions{
  public static void GetRenderings(){
    var args = new GetXmlBasedLayoutDefinitionArgs();
    CorePipeline.Run("mvc.getXmlBasedLayoutDefinition", args);
    var xmlDefinition = args.Result;
    //Get the context device definition
    var deviceNode = xmlDefinition.XPathSelectElement("d[@id='" + (object)Sitecore.Context.Device.ID.ToString() + "']");
    XmlDocument deviceNodeXML = new XmlDocument();
    deviceNodeXML.LoadXml(deviceNode.ToString());
    // Exracts rendering from the context device
    Item contextItem = Sitecore.Context.Item;
    var renderingReferences = 
    LayoutField.ExtractReferences(deviceNodeXML.FirstChild, contextItem.Language, contextItem.Database);
    // code to interate through each renderings
    foreach (var renderingReference in renderingReferences)
    {

    }
  }
}

By calling above snippet SitecoreHelperExtensions.GetRenderings(), you can get all the rendering on an item merged with page/partial design renderings.

Hope this helps!!!

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.