Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get content of rendering programmatically?

I am attempting to write a method that will output the content (i.e. HTML) for any renderings that happen to exist within a specific placeholder. The goal is to pass in a Sitecore.Data.Items.Item and the placeholder key that i'm interested in, and the method should return the rendered content.

The issue with this seems to be that there is no page context established, and therefore calling RenderControl() is throwing a null reference error in the GetCacheKey() method of the Sublayout.

Is anyone aware of a way to render a Sublayout or XSLT rendering programmatically?

Here's what I've got so far:

private string GetPlaceholderContent(Item item, string placeHolder)
{
    StringWriter sw = new StringWriter();
    using (HtmlTextWriter writer = new HtmlTextWriter(sw))
    {
        foreach (RenderingReference renderingReference in item.Visualization.GetRenderings(Sitecore.Context.Device, false))
        {
            if (renderingReference.Placeholder == placeHolder)
            {
                // This ensures we're only dealing with Sublayouts
                if (renderingReference.RenderingItem.InnerItem.IsOfType(Sitecore.TemplateIDs.Sublayout))
                {
                    var control = renderingReference.GetControl();
                    control.RenderControl(writer); // Throws null reference error in GetCacheKey()
                }
            }
        }
    }

    return sw.ToString();
}
like image 343
Derek Hunziker Avatar asked Mar 06 '13 22:03

Derek Hunziker


1 Answers

It is almost 8 years since the question was originally asked, and it turn to be Uniform - rendering any item/placeholder fragment!

Yes, it cuts the item you supply into placeholders/renderings:

enter image description here

The next step is to produce markup (for every possible data source out there):

enter image description here

That content is published into CDN and browser picks which version to load = personalization works!

In other words, the question you've asked turned into a cutting-edge product that can do so much more on top of that!

You could reverse-engineer the Uniform assemblies to see how they actually do that ;)

like image 76
Nikolay Mitikov Avatar answered Oct 03 '22 01:10

Nikolay Mitikov