4
votes

I want to get all the renderings of a content item and render each of them to html string inside an MVC action using C# code. Below is the code I am using to get all the renderings of a content item.

Item item = Sitecore.Context.Database.GetItem(someItem);
RenderingReference[] myRenderings = item.Visualization.GetRenderings(Sitecore.Context.Device, true);

foreach (RenderingReference rendering in myRenderings)
{
    RenderingItem renderingItem = rendering.RenderingItem;
}

I am able to get this list and the rendering item's Id. But how can i render them to html string here?

Note: the renderings could be of any rendering type like view renderings, xsl renderings , controller renderings etc. I dont want to use the approach of WebClient or HtmlAgility pack.

1

1 Answers

1
votes

Not sure what you really try to do, but this code allows you to render a rendering in a MVC action and returns the Html for this:

public ActionResult GetFirstRenderingHtml()
{
    var rendering = PageContext.Current.PageDefinition.Renderings.First();
    return this.View(new RenderingView(rendering));
}

This actually only returns the code of the first rendering of the current item. If you want to call this action directly, you need to add the sc_itemid parameter in the to specify the context item:

/api/yourcontroller/GetFirstRenderingHtml?sc_itemid=<item id>

If you want the Html for a complete item with all it's renderings, I suggest you create a web request to get the output.