1

We are using Sitecore 9.1 MVC.

It is possible to get all rendering references for a given item by using:

item.Visualization.GetRenderings(Sitecore.Context.Device, true)

However, this will give me an array of RenderingReference.

The module I am developing, needs to run this pipeline:

PipelineService.Get().RunPipeline<RenderRenderingArgs>("mvc.renderRendering", new RenderRenderingArgs(rendering, writer));

Which requires a Rendering object and not a 'RenderingRef' object.

How do I get the corresponding Rendering from RenderingRef?

1
2

As pointed out by @Abhishek Malaviya, there was an answer at this reference:

https://www.cmsbestpractices.com/sitecore-item-rendering-best-practice/

For completeness sake, this is the code that creates a Rendering from a RenderingRef:

var renderings = new List<Rendering>();

var refs = Item.Visualization.GetRenderings(Context.Device, false);
if (!refs.Any())
  return null;

var renderingReferences = refs.Where(r => !(Context.Database.GetItem(r.RenderingID).TemplateID.ToString() == "{86776923-ECA5-4310-8DC0-AE65FE88D078}" && string.IsNullOrWhiteSpace(r.Settings.DataSource))).ToList();

if (!renderingReferences.Any())
  return null;

renderings.AddRange(renderingReferences.Select(r => new Rendering
{
    RenderingItemPath = r.RenderingID.ToString(),
    Parameters = new RenderingParameters(r.Settings.Parameters),
    DataSource = r.Settings.DataSource
}));
0

You can get corresponding renderings of item using below code :

 string deviceName = "Default".ToLower();
            var deviceItem = item.Database.Resources.Devices.GetAll().Where(d => d.Name.ToLower() == deviceName).First();
            if (deviceItem != null)
            {
                var renderings = item.Visualization.GetRenderings(deviceItem, false);
                if (renderings != null)
                {
                     foreach (var renderingref in renderings)
                    {

                    }                   
                }

            }
1
  • That will give me an array of RenderingRef. What I would like is the Rendering object, not the RenderingRefs :)
    – Hos
    May 9 '19 at 4:57

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.