0

I've created a Parameters Template with a Treelist Field and a Rendering which uses this Template. That works so far I can select multiple values. But Now I am using this rendering in a dynamic Placeholder and I want to get the Selected Values from my Parameter. The Only thing I get at the moment are Pipe separated GUIDs and I was hoping that I could use MultilistField to get the Values from my selection. But I don't know how to initialize the Multilistfield. Because the Guids I am currently getting with the following Function

public string GetParameter(DynamicPlaceholderRenderContext context, string parameter)
{
        var parameterValue = context.Parameters.ContainsKey(parameter) ? context.Parameters[parameter] :
             context.Rendering.Parameters.Contains(parameter) ? context.Rendering.Parameters[parameter] :
             string.Empty;

        return parameterValue;
 }

which I Call in my view inside the wrapper Method fpr my DynamicPlaceholder

 @Html.Sitecore().DynamicPlaceholder("grid-column", CreateWrapper)

 @functions
 {

     private readonly IParameterTemplatesProvider _parameterTemplatesService = new ParameterTemplatesProvider();

     HtmlString CreateWrapper(HtmlString htmlString, DynamicPlaceholderRenderContext context)
     { 
        var cssClasses = _parameterTemplatesService.GetParameter(context, Templates.GridBase.Fields.CssClassesFieldName);


     }
 }

When I try to get some "context" data like this

var test = Sitecore.Context.Item.Fields[Templates.Column.Fields.CssClassesFieldName];

I get null back but no Object or something else where I can create a MultilistField. I Think I've some prober missunderstandings when I can access

Sitecore.Context.Item or RenderingContext.Current.Rendering.Item how can get the Real Selected values from my Treelist

I am not shure if my problems are associated with DynamicPlaceholders or some other missunderstandings in the sitecore technology :-)

1 Answer 1

1

Rendering parameters are stored as name-value-collection (like a query string) with just the raw value - which for a multi-list field is a pipe-separated list of IDs.

You could change your GetParameter() method to something like the following, to get a list of items:

public IList<Item> GetParameter(DynamicPlaceholderRenderContext context, string parameter)
{
    var parameterValue = context.Parameters.ContainsKey(parameter) ? context.Parameters[parameter] :
            context.Rendering.Parameters.Contains(parameter) ? context.Rendering.Parameters[parameter] :
            string.Empty;

    // You might want to do some more checks here
    // - check for valid IDs, non-null items returned etc.
    return parameterValue.Split(new [] {'|'}, StringSplitOptions.RemoveEmptyEntries)
                         .Select(id => Sitecore.Context.Database.GetItem(id))
                         .ToList();
}

As noted you might want to do some error-checking just in case.

3
  • So afte this I get the Items I think that works now, but I am not able to get the Value from my Items. I've tried to run throug my return values with a foreach and then use "item[Templates.Column.Fields.CssClassesFieldName]" to get the value but I never get any value back Oct 8, 2018 at 8:57
  • 1
    It sounds like you might be confusing the field name of the rendering parameter field (for the multi-list) and the field name on the actual CssClass item with the actual value. Oct 8, 2018 at 9:58
  • Yes that was the Problem, some naming Problems :-/ and I've tried to get the values from the wrong Object. It's hard that you can't see which values are assinged to an item in the debugger then I should have found my problem earlier Oct 8, 2018 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.