Sitecore: How to use sublayout parameters from codebehind?

前端 未结 2 925
醉梦人生
醉梦人生 2020-12-11 18:43

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

相关标签:
2条回答
  • 2020-12-11 19:00

    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.

    0 讨论(0)
  • 2020-12-11 19:03

    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.

    0 讨论(0)
提交回复
热议问题