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.

Add comment


(Will show your Gravatar icon)

  Country flag
Click to change captcha
biuquote
  • Comment
  • Preview
Loading