Links

Weblog

« Sitecore Logging | Home | Sitecore Crestone »


18 December 2007

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...

10:40 Posted in Sitecore | Permalink

Comments

Cool.

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

Posted by: Lars Nielsen | 18 December 2007