Communicating between Sublayouts

Posted 09/08/2016 by Subramanian Ramanathan

We often come across scenarios where we need to communicate between sublayouts while working with webforms based solutions. Here are a few solutions for solving this problem.

It's possible to do this using Sitecore’s Events by raising an event in one sublayout and handle it in another.

Sitecore.Events.Event.RaiseEvent("YourEventName", new YourEventArgsClass { Property = "SomeValue" });
Sitecore.Events.Event.Subscribe("YourEventName", eventHandlerRef);

But the problem here is, whenever we raise an event, it will not work on the existing instance of a Sublayout that is rendered on the page, instead a new instance of the Sublayout will be created. So we were not able to assign/capture the values in the page control.

Another possible solution would be getting the renderings of the current page using RenderingReference or using Layout Deltas. 

We also tried creating an Enumerator and using the FindControl method.

However, after extensive research, we found a much more simpler and straightforward Sitecore API for this in WebUtil class.

SublayoutItem anotherSublayoutItem = new SublayoutItem(Utilities.GetItem(…));
Sublayout[] subLayouts = Array.ConvertAll(Sitecore.Web.WebUtil.FindControlsOfType(typeof(Sublayout), Page), i => (Sublayout)i);
Sublayout anotherSublayout = subLayouts.Single(s => s.Path.Equals(anotherSublayoutItem.FilePath));
Control userControl = anotherSublayout.GetUserControl();

This again works like an Enumerator and gets the controls of specified type, but will save us some lines of code and time. We can also find the asp/telerik/mvc control which is required, using this method.
FindSubControlsOfType is another method of WebUtil which may also be helpful.


Add your comment