Pages

Monday 11 March 2013

Include managed meta data in your core search results web part SharePoint 2010

Issue:

So you have been asked to display managed metadata fields in your core results. These are the general steps I follow:

1: Add the managed metadata property to the Managed Property in Search Administration in CA. Here is how to do this: follow this blog post.

2: After you run a full crawl (ensure you have at least 1 item with data populated), we need to modify the core results web part to display the information. For example purpose we will call our field Visible-Internal (this is basically the department field which is a managed metadata field)

3: First we need to ensure that the field is actually being pulled: So navigate to your search page, edit the page and edit the search core results web part. Under Display Properties > Untick "Location Visualisation" > Click on XSL Editor, copy and save the existing text and then modify with the following code:

I generally use Notepad++ to make any changes

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xmp><xsl:copy-of select="*"/></xmp>
</xsl:template>
</xsl:stylesheet>


The above code displays all the fields that search is indexing, ensure your field is showing up in here, in our instance it is <visible-Internal>DATA</visible-internal>

4: Now for the fun part (well not so fun part), we need to format the results, by default Managed metadata is displayed as follows:

ID;#Value|GUID

This is fine but in order for a richer search experience we will look at formatting the results so we only display the "Display Name: VALUE"

5: Open XSLT editor and replace with the saved Text in Step 3

6: Add this to the top section of the XSLT code:
<xsl:param name="FieldName" /> in our case it is <xsl:param name="Visible-Internal" />

7: Under XSL:Choose add the following field:
            <xsl:if test="string-length(visible-internal) &gt; 0">  
                 <xsl:text disable-output-escaping="yes">&#8195;</xsl:text>
                 <xsl:value-of select="$visible-internal" />
                 <xsl:value-of select="visible-internalr" />
             </xsl:if>


8: Add this under <div class="srch-Metadata2">
    <xsl:call-template name="visible-internal">
    <xsl:with-param name="visible-internal" select="visible-internal" />
    </xsl:call-template>


9: Search for <xsl:template name="DisplayAuthors"> and add the following code:

 <xsl:template name="Visible-Internal">
    <xsl:param name="visible-internal" />
    <xsl:if test='string-length($visible-internal) &gt; 0'>
     Visible to:<xsl:value-of disable-output-escaping="yes" select="substring-after(substring-before($visible-internal, '|'),';#')"/>
     <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>
     <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>
    </xsl:if>
</xsl:template>


The magic happens in substring-before and substring-after.

10: Copy and paste it in the XSL editor and save the properties, you will have to save the page and close. Once you refresh, you should see your data as Visible to: Internal Staff

Hope this helps and please share your feedback.

No comments:

Post a Comment