0

I need to get the rendering parameters programmatically from my sublayout. Currently I do this as such:

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);

This solution came from this question and works perfectly until I have two sublayouts of the same type on the page. When this occurs obviously renderings.FirstOrDefault(r => r.RenderingItem.InnerItem["Path"] == sublayout.Path); always returns the first rendering parameter that matches the sublayouts path for both sublayouts.

How can I differentiate between them? I can see nothing that I can use to tie them together!


Edit:

To be clear, I add my sublayout in Presentation > Details, then when I click my control I set the fields in the 'Control Properties' window. I have a field called Module Source which always comes back the same - it always populates as the one highest up in the order. The values are definitely different for each sublayout but I cannot get them from the renderings.

4

2 に答える 2

1

Not sure if I'm missing something. But you can get the sublayouts rendering parameters, directly on the Sublayout. I use the following on my base Sublayout I use for all my Sitecore sublayouts - and it has no problems with rendering parameters on the same sublayout inserted multiple times :)

    protected Sitecore.Web.UI.WebControls.Sublayout CurrentSublayout
    {
        get
        {
            Control c = Parent;
            while (c != null && !(c is Sitecore.Web.UI.WebControls.Sublayout))
            {
                c = c.Parent;
                if (c == null)
                    break;
            }

            return c as Sitecore.Web.UI.WebControls.Sublayout;
        }
    }

    protected NameValueCollection CurrentParameters
    {
        get
        {
            if (CurrentSublayout == null)
                return null;

            NameValueCollection parms = WebUtil.ParseUrlParameters(CurrentSublayout.Parameters);

            var sanitizedValues = new NameValueCollection();
            for (int i = 0; i < parms.Count; i++)
            {
                if (!string.IsNullOrEmpty(parms[i]))
                    sanitizedValues.Add(parms.Keys[i], parms[i]);
            }

            return sanitizedValues;
        }
    }
于 2012-05-16T12:48:11.707 に答える
0

You may want to check the cache settings on your sub-layout, if you don't have Cacheable VarbyParam it is not going to work for you

于 2014-07-25T17:59:41.563 に答える