Display a custom property in the results list

| December 17, 2011

A while back I answered a question on LinkedIn about how to display the ContentType in the result list. Since then, now and again, people ask me how to display properties in the result list. Basically, the same method can be used for any property so I am posting the solution here. Just change CType in the following example to the property of your choice and you will get it displayed. Good luck with it.

Robert

 

 

* By Document Content Types, I imagine you probably want to get SharePoint Content Types such as Document, List, Item and other ones you have defined, so I would recommend creating a new Managed Property in SharePoint’s Search Service Application under Metadata Properties. I would name it CType and map teh crawled property ows_ContentType(Text) crawled property to it. Re-crawl. You can use the existing ContentType property but I like to map my own properties so I am sure I am not getting back things from the mysterious Basic:5.

* Go to the search result page and edit the page and then the Core Results web part. On the right expand the Display Properties section. Uncheck Use Location Vizualization. Add a new column to be fetched from the index. Call it <Column Name=”CType”/>. Place it anywhere in the list as long as it is after the first <Columns> tag and after the closing columns tag.

* Click XSL Editor… You can use the existing DisplayString template or create your own at the bottom of the XSLT. I would add my own:

<xsl:template name=”DisplayCType”>
<xsl:param name=”ctype” />
<xsl:if test=’string-length($ctype) &gt; 0′>
Type:
<xsl:value-of select=”$ctype” />
</xsl:if>
</xsl:template>

Just before the end stylesheet comment.

* Place a call to this template where you want it displayed. I would put it with the other properties at the end of the srch-metadata2 div section, just after size. Use this:

<xsl:call-template name=”DisplayCType”>
<xsl:with-param name=”ctype” select=”ctype” />
</xsl:call-template>

1. Save and hit ok. If the property was crawled you should have the content type displayed at the end of the properties.

If it didn’t work, you may not have the values in the results. Save your XSL and replace it in the same place with the following:

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

This will put out the values fetched from the index in XML and you can see if your CType value is returning anything. If it isn’t, go back and do a full crawl again.

Robert
www.prosharepointsearch.com

Category: Developer Articles

Comments are closed.