Using content search webpart and item templates. I want to have an easy way for the user to pass a custom value into the item template, without having to open up the code and edit the template itself.
I found that I can add a mapped property, for example I want to have a user controlled maximum number of lines. At the top of my template in the ManagedPropertyMapping node I add this:
That creates a nice user interface for end users who Edit this webpart and want to change the property mappings.
Then in my code in the item template I access the value of that "property mapping" like this:
var propMapStr = ctx.ClientControl.get_propertyMappings(); var LineLimit = (propMapStr == "") ? ctx.DisplayTemplateData.ManagedPropertyMapping.MaxLines : Srch.Result.parsePropertyMappingsString(propMapStr).MaxLines;
That checks to see if they have customized the values and if so returns that or returns the default if not.
From there I can use the LineLimit in my code and/or in my OnPostRender. All this is working, but I have to admit it is pretty ugly.
My are my questions:
Is there a better more SharePoint-y way of doing this?
Can anybody see a reason that this hack shouldn't be used?
It seems really messy to have to check if there are customized values, is there a way to just get "here are the property mapping values I should use"?