Sitecore: How to use sublayout parameters from codebehind?
Asked Answered
B

2

6

How do I get the values from the "Parameters" field (second screenshot) in the code-behind of the sublayout?

I understand I can get/set parameters on a rendering (specifically sublayout) when it is added to the presentation details of an item, just as described here (Sitecore 6 - using parameters).

layout instance parameters

However I would like to use the parameters field from the layout definition item. In the codebehind of the file belonging to to layout definition I can cast the parent to a sublayout and that object also has a .Parameters property, however this doesn't contain the values I'd expect.

layout definition parameters

This is the Page_Load method in the control code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    var sublayout = ((Sublayout)this.Parent);
    string rawParameters = Attributes["sc_parameters"];
    NameValueCollection parameters =
      Sitecore.Web.WebUtil.ParseUrlParameters(rawParameters); 
      //parameters contains values from "Additional parameters (first screenshot)

      //I do not know the sublayout item id or sublayout path, so how do I get
      //the values from the second screenshot?
}

Doublecheck still doesn't work, only additional parameters are shown:

Step 1 - Enter parameters

Step 2 - Add sublayout + parameters to presentation

Step 3 - Display parameters on sublayout

Step 4 - Validate result

Blimp answered 10/5, 2011 at 11:20 Comment(4)
What are you really trying to do here? I don't understand the point of accessing this field since it will be static across all the items which use the rendering. You might as well hard-code the value in your sublayout .ascx, or an AppSetting value... Or if you need to get at it in Content Editor, then in the Standard Values of presentation details for your item template.Vesuvius
Hardcoding the data might be a solution. I'm using the sublayouts as a container for another file and need to specify this filepath and some more data.Blimp
Just to be clear. You want to get the Parameters from the SublayoutItem and not from the Sublayout definition set in the layout details field of an item. Is that right?Titanium
@marto: Actually I want both...Blimp
T
2

You can get the parameters defined on the sublayout but it's a bit long winded. You need to find the correct rendering item first and from there retrieve the parameters

   var sublayout = ((Sublayout)this.Parent);
   //Get all rendering
   var renderings = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);

   //Get the first rendering that matches the current sublayout's path
   var sublayoutRendering = renderings.FirstOrDefault(r => r.RenderingItem.InnerItem["Path"] == sublayout.Path);

   if (sublayoutRendering != null)
         Response.Write(sublayoutRendering.RenderingItem.Parameters);

You can use Mark's way to get the parameters for the parameters set in the "Layout Details"

EDIT: The above solution will work but it's very fragile and depends on sitecore internals that might change in the future. I wouldn't recommend you go with it in production with it. There must be a better way to achieve what you want.

Titanium answered 11/5, 2011 at 15:51 Comment(1)
This is a solution yes, however I am looking for something more solid and perhaps built-in...Blimp
B
12

Like this:

var sublayout = ((Sublayout)this.Parent);
NameValueCollection nvc = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters);

Here's a blog post that make it easier to do with extension methods.

Here's a shared source module for Sitecore that wraps this up in a class as well. It was written by John West, CTO of Sitecore USA.

Broccoli answered 10/5, 2011 at 11:38 Comment(9)
The Parameters property and the "sc_parameters" attribute value are the same, this is not what i'm looking for. I'm trying to get the layout definition parameters, please see my attached images...Blimp
For some reason I believe what you put in there is passed across to an instance of that component being used. So if you add foo=bar then on a sublayout bound to an item, that will be part of the parameters. The label for the parameters in the first screenshot is called "Additional Parameters" which makes me believe they're in addition to what you've added to the multi-line text field of the layout definition. Can you confirm this via code? Remember that the parameters need to be in query string name-value format.Broccoli
This is not the case. I first thought the same, however debugging shows me it's not the case. Actually some values, belonging to a template are passed?? I attached a custom template as base template of the Standard template. The fields/values in this custom template are sent as parameters...?!?Blimp
I just tested it out and it works as I expected. I'm not sure how its not for you. I entered a=b&c=d in the MLT Parameters field (your second screenshot) of the sublayout definition. On a page where I use the sublayout I access the parameters via the code and I get these values.Broccoli
Please see my 'doublecheck' in the question. It really doesn't work as expected...Blimp
That's odd as that's not how it behaved for me. It could be an issue with your specific version of Sitecore.Broccoli
Using 6.4 here, what's your version?Blimp
I just downloaded the recommended version (6.3). It's the same... However what I did find out is that the parameters are overridden. So if you do not define parameters on the instance, then the 'master' parameters are passed, otherwise only the instance parameters are passed... grrrrBlimp
Ahhh, that explains why it worked for me. I never defined any additional parameters on the instance itself.Broccoli
T
2

You can get the parameters defined on the sublayout but it's a bit long winded. You need to find the correct rendering item first and from there retrieve the parameters

   var sublayout = ((Sublayout)this.Parent);
   //Get all rendering
   var renderings = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);

   //Get the first rendering that matches the current sublayout's path
   var sublayoutRendering = renderings.FirstOrDefault(r => r.RenderingItem.InnerItem["Path"] == sublayout.Path);

   if (sublayoutRendering != null)
         Response.Write(sublayoutRendering.RenderingItem.Parameters);

You can use Mark's way to get the parameters for the parameters set in the "Layout Details"

EDIT: The above solution will work but it's very fragile and depends on sitecore internals that might change in the future. I wouldn't recommend you go with it in production with it. There must be a better way to achieve what you want.

Titanium answered 11/5, 2011 at 15:51 Comment(1)
This is a solution yes, however I am looking for something more solid and perhaps built-in...Blimp

© 2022 - 2024 — McMap. All rights reserved.