web 2.1

Accessing parameters in Sublayouts

Currently I'm working on a project where we utilize the Parameters on a Sublayout-assignment to "attach" metadata to the sublayout. It's definitely not a standard Sitecore way to do it - however the project required this approach.

We then ran into a small problem. In Sitecore 5.3.1 there's actually no way to access the parameters from the Sublayout.... Yes way! Its a minor "left-out" by Sitecore, and they probably didn't notice it since almost no one uses the parameters.

Thanks to Kim Hornung (Sitecore) the problem was quickly resolved. Heres the solution:

1. Create a Class named "SublayoutWithParameters".

public class SublayoutWithParameters : Sublayout, IHasParameters
{
   /// <summary>
   /// Adds the parameters.
   /// </summary>
   /// <param name="parameters">The parameters.</param>
   public void AddParameters(string parameters)
   {
      Parameters = parameters;
   }
}

2. Replace the Sitecore class in web.config:

<!--control template="sublayout" type="Sitecore.Web.UI.SublayoutRenderingType, Sitecore.Kernel" propertyMap="Path=path" /-->
<control template="sublayout" type="MyAssembly.SublayoutWithParameters, MyAssembly" propertyMap="Path=path" />

3. Now you are able to access the Parameters passed by Sitecore in your Sublayouts:

protected override void OnInit(EventArgs e)
{
  if (Parent is WebControl)
   {
     WebControl parent = Parent as WebControl;
      // parent.Parameters is a string containing the parameters specified in Sitecore (formed as a QueryString)...
      string param = HttpUtility.UrlDecode(parent.Parameters);
   }
  base.OnInit(e);
}

That should be it...

Tags:

Categories: Sitecore

Comments

Lars Nielsen Denmark, on 12/18/2007 11:20:12 AM Said:

Lars Nielsen

Cool.

I've asked this feature to be implemented into Sitecore by default in future.

koreyweldon.wordpress.com , on 4/4/2011 6:18:41 PM Said:

pingback

Pingback from koreyweldon.wordpress.com

Man steers R/C car with his hands, not to mention an HTML5-based web app (video) | koreyweldon

koreyweldon.wordpress.com , on 4/6/2011 12:57:47 AM Said:

pingback

Pingback from koreyweldon.wordpress.com

Meet the con artist who spent $3 million of Best Buy’s money on supercars [Feature] | koreyweldon

koreyweldon.wordpress.com , on 4/11/2011 8:55:35 AM Said:

pingback

Pingback from koreyweldon.wordpress.com

Delkin Elite 633 claims to be the fastest SDHC card with 80MBps write speeds | koreyweldon

koreyweldon.wordpress.com , on 4/12/2011 12:00:05 PM Said:

pingback

Pingback from koreyweldon.wordpress.com

New software uses facial recognition to defend against prying eyes | koreyweldon

mkbed-04.miltonkeynesbedsits.co.uk , on 4/16/2011 1:15:42 AM Said:

pingback

Pingback from mkbed-04.miltonkeynesbedsits.co.uk

Mary J. Blige Dishes On Her ‘Crazy’ Led Zeppelin Covers «  miltonkeynesbedsits.co.uk

koreyweldon.wordpress.com , on 4/19/2011 2:55:56 PM Said:

pingback

Pingback from koreyweldon.wordpress.com

Motorola Xoom Seems Destined for AT&T [Unconfirmed] | koreyweldon

koreyweldon.wordpress.com , on 4/20/2011 5:58:37 PM Said:

pingback

Pingback from koreyweldon.wordpress.com

Cueboy Quest is an adorable 8-bit style physics game | koreyweldon

Comments are closed