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

Sitecore Logging

Hi all!

It has been a while since my last post – I know. As everybody else, my schedule has been filled with projects so my blogging time has been very limited. This post will also be very short - just thought I’d share a little trick for those of you tired of scrolling through Sitecore log files.

The small utility BareTail auto-updates, highlights etc. So far it hasn't locked the log-files preventing Sitecore (log4net) from writing entries.

Find it here: http://baremetalsoft.com/baretail/index.php

All for now...

Anders

Tags:

Categories: Sitecore