SharePoint 2013 – Refinement Panel showing string;# for calculated text fields

Today I had the same problem as described in the following forum post:
http://social.technet.microsoft.com/Forums/en-US/260afea2-45e4-41f6-879d-bff7a7ac7e8f/using-calculated-columns-in-the-search-refinement-panel

refinement

So first I looked into how the refinement panel works.

There is a Display template called Vertical (Control_Refinement.html and Control_Refinement.js) that creates the refinement panels

Vertical

Then when I ran the Vertical javascript in my debugger I figured out that the Filter_Default.js controls the format of all the refinement panels.

Ok, so I do’t want to modify any out of the box files so I created a copy of the Filter_default.js. All I now need to do is reference it … Hmm, where is that done then?

I exported the refinement web part and within the SelectedRefinementControlsJson property it is possible to specify the filter script that should be used.

 

<webParts>
<webPart xmlns=”http://schemas.microsoft.com/WebPart/v3″&gt;
<metaData>
<type name=”Microsoft.Office.Server.Search.WebControls.RefinementScriptWebPart, Microsoft.Office.Server.Search, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>

<property name=”SelectedRefinementControlsJson” type=”string”> …..     “displayTemplate”:”~sitecollection/_catalogs/masterpage/Display Templates/Filters/Filter_Default.js”

….

<property name=”Direction” type=”direction”>NotSet</property>
</properties>
</data>
</webPart>
</webParts>

Ok, so that is easy…  Export, change the display template for the affected filter and import the web part again ( isn’t there a property available in the interface). Yes of course there is. I just missed it the first time around. Edit the web part Then select Choose Refiners … and select the Display template

So all I now have to do is download the Filter_default and fix it and upload it to my display templates.Only about 450 lines of JavaScript in there.

Near line 232 you will find the following code

for(var i in listData)
{
listData[i].RefinementTokens = [listData[i].RefinementToken];
listData[i].RefinementTokenWrappedValues = [Srch.RefinementUtil.stringValueToEqualsToken(listData[i].RefinementValue)];
if(ctx.RefinementControl.propertyName == “MediaDuration”)
{
Srch.U.modifyMediaDurationRefinementName(listData[i]);
}
}

Add the following two lines:

listData[i].RefinementName=listData[i].RefinementName.replace(“string;#”,””);
listData[i].RefinementValue=listData[i].RefinementValue.replace(“string;#”,””);

giving the following result:

for(var i in listData)
{
listData[i].RefinementName=listData[i].RefinementName.replace(“string;#”,””);
listData[i].RefinementValue=listData[i].RefinementValue.replace(“string;#”,””);
listData[i].RefinementTokens = [listData[i].RefinementToken];
listData[i].RefinementTokenWrappedValues = [Srch.RefinementUtil.stringValueToEqualsToken(listData[i].RefinementValue)];
if(ctx.RefinementControl.propertyName == “MediaDuration”)
{
Srch.U.modifyMediaDurationRefinementName(listData[i]);
}
}

And now your refinement panel looks as expected without the string;#

16 thoughts on “SharePoint 2013 – Refinement Panel showing string;# for calculated text fields

  1. Hello, thank you for your interesting blog. I was wondering, related to the refinement panel, if you should have a solution to enable the MUI multilanguage of the refinement values. Apparently, out of the box, this is not possible in SP2013. I’ve found a custom webpart somewhere: http://ventigrate.codeplex.com/wikipage?title=MUI%20Search%20Refinement%20Panel&referringTitle=Home%22 but it doesn’t seem to work in 2013. Any tips or hints ?

      1. Hey Pieter,

        thanks for your reply.

        I’ve adjusted a couple of display templates and am familiar with developing with the client-side OM so i guess this must be do-able 🙂 I’ll give it a shot !

  2. Hi Pieter

    I believe there is a small mistake in your solution. The second line should be:
    listData[i].RefinementValue=listData[i].RefinementValue.replace(“string;#”,””);

  3. Hi Pieter. This is a great post! I have a custom search page created that uses refiners of calculated columns for the month and the year. I used your fix above and it converted the refiner for the Month column correctly but the string;# is still in front of the Year column. Any ideas?

    1. Hi Heather,

      The easiest way to check what is going wrong:

      1. load the page in IE
      2. Run in Debug mode (F12)
      3. find the “listData[i].RefinementName=listData[i].RefinementName” lines in your javascript.
      4. set a break point on the above line
      5. Check what listData[i].RefinementName is set to before and after the above line.

      I hope this helps.

      Pieter

    2. Update: I actually found the problem. When choosing the refiner, one of the options is multi-value refinement item or refinement item. I didn’t notice that refinement item was listed as an option 2 times for some reason. For the month, I had the 2nd one chose but for the year, I had the first one chosen. When I selected the 2nd (refinement item) for the year, it corrected the formatting and removed String;# from the fields.

    1. Sorry for the delay in responding. Did you get your issue resolved. There shouldn’t be too much of a difference for multiple or single value refinements. in both cases the string;# can be cleaned up in the same/similar way.

  4. Hi Pieter,

    I did the same and its working fine in Firefox. But it is not working in IE. Any suggestion?

    1. Could you stick your browser debugger on and set a breakpoint in the display template on the for(var i in listData) line? That should give you an idea of what is going wrong.
      Please let me know if this helps or not.

  5. Hi, thanks for this. It solved my problem but I had to tweak the template and input the extra code using single quotes only as below:

    listData[i].RefinementName=listData[i].RefinementName.replace(‘string;#’,”);
    listData[i].RefinementValue=listData[i].RefinementValue.replace(‘string;#’,”);

    Again thanks for taking the time to post this fix.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: