Sitecore: Get parent rendering parameters

3.3k Views Asked by At

Consider this scenario in Sitecore MVC: I have a rendering called Rendering A which is a DIV with a placeholder inside (placeholder-a). Then I have a second rendering, named Rendering B, which I place inside of placeholder-a.

Is there a way that my code for Rendering B can read the rendering parameters of Rendering A?

4

There are 4 best solutions below

0
On

Out of the box it is impossible due to calling of RenderRendering pipeline separately for each rendering in placeholder.

I did it by overriding placeholder control and allowing to pass additional parameters via placeholder.

e.g.

@Html.SitecoreExtended().Placeholder("extendedForRendering", new {ExampleRouteValue1 = "Sitecore MVC Placeholder with rendering view"})

In this case I was able to transfer required parameters via placeholders from parent view to child view.

0
On

As Anton mentioned you cannot get the parent rendering but you can however get a list of all renderings on the current page as follows:

var pageContext = Sitecore.Mvc.Presentation.PageContext.CurrentOrNull;
if(pageContent!=null)
    var renderings = pageContext.PageDefinition.Renderings;

Once you have the renderings you can look up the placeholder values, parameters etc. Another possibility, not sure if this works for you, is that from the current rendering context you can always get the children renderings. Hope this helped.

0
On

I know that this is a too old question, but I have the same issue. If you use dynamic placeholder, sitecore add to the placeholders name Guid, something like this "placeholder-name_{GUID}_number-of-item-in-this-placeholder". So, UniqueId of rendering where you have your Placeholder-A is that Guid that sitecore use for your dynamic placeholder

Example

0
On

Seeing as Rendering A code is executed first, could you possibly add the rendering parameters in the session / cache? and then access it in Rendering B?