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:

'MaxLines'{Line limit}:'20'

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"?

  • Stuff a JSON object into that one string-field, and parse it back with JSON.parse in your template. Other method would be to pass values in the URL QueryString. But lets be honest.. the whole DisplayTemplates approach of stuffing JS into HTML comments IS ugly, and with ShareePoint Fx technology going to be obsolete. General rule... if it works and its JavaScript it works... might be ugly.. but it works – Danny '365CSI' Engelman Sep 8 '16 at 19:28
  • But wouldn't I need to use the same technique to retrieve the json string? I don't see how I could pass info in the url since there might be multiple item templates on the page with different limits. What is SharePoint Fx? – Rothrock Sep 8 '16 at 20:16
  • Yes, kinda like the stuff you are doing now. SPFx: Microsoft is moving everything to Fronnt-End technologies.. that means End-Of-Lifecycle of DisplayTemplates (my interpretation) dev.office.com/blogs/… – Danny '365CSI' Engelman Sep 8 '16 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.