Wednesday, October 6, 2010

Sitecore CMS 6.2 - Accessing parameters in Sublayouts

<Rant>I often seem to have no end of grief when searching for Sitecore documentation straight from Google. It seems most of the useful information is buried in cookbooks and component references which are in PDF format rather than HTML and require a login to the Sitecore Developer Network. I just find it frustrating that they have lots of good documentation but it is difficult to search. I'm almost tempted to bring all their docs down to a public facing web server and letting it get indexed.</Rant>

As a case and point, I wanted an administrator editing the layout details for a content items control to be able to specify a value that would be mapped into a public property on a user control. The Additional Parameters seemed like a good candidate but didn't lead anywhere with the standard Sitecore install.

Turning to Google I found Anders Dreyers post on Accessing parameters in Sublayouts with Sitecore 5.3.1. At least with that version of Sitecore it looked like parameters weren't easily available in a sublayout.

That post did lead me to Sitecore.Web.UI.SublayoutRenderingType, which is referenced in the web.config as the rendering control for sublayouts. Using Reflector on it shows that the parameters collection could be used to automatically set properties using reflection.

After searching through the Presentation Component Reference I went to the Presentation Component Cookbook PDF via a footnote reference and then onto the SublayoutParameterHelper via another footnote.

4.5.3 How to Pass Rendering Parameters to a Control
To pass parameters to a control using the Control Properties dialog:
  • In the Control Properties dialog, enter control properties.
To pass parameters to a control using the rendering properties dialog:
  • In the rendering properties dialog, click the Parameters tab, and then enter named parameters.12
12 To access the parameters passed to a sublayout, see http://trac.sitecore.net/SublayoutParameterHelper.

Googling for SublayoutParameterHelper lead me back to Cascade the Data Source of a Sublayout down to Nested Controls PDF, which seems to explain how to use the SublayoutParameterHelper.

This looks like just what I was after. By inheriting from Sitecore.Sharedsource.Web.UI.Sublayouts.SublayoutBase rather than System.Web.UI.UserControl the parameters will be automatically mapped to the corresponding properties in the sublayout if they exist. Just remember to call base.OnInit() if you override it (as per the documentation).