AlertThis post is over a year old, some of this information may be out of date.
How to Display the Site Title and Site URL in Search Results
August 3, 2011
Recently I was restyling the search results, and one of the expectations was to display a hyperlink to the site of origin of the result.
Adding the site title of the returned results is very easy, this can be retrieved from the metadata property “SiteTitle”. The web URL is a bit more difficult. My first thought was to check the crawled properties, but there is no reference for the web URL, only a reference for the site collection URL called: “ows_SPSiteURL”.
My second thought was to some XSL manipulation, and this was the best approach.
The final result will be:
Search Result with Site Reference
The things I did to establish this were:
Adding Site Title column references to the fetched properties of the Core Result Web Part.
Editing the XSLT from the Core Result Web Part.
In the following sections I will go deeper into the two steps.
Adding column references (Fetched Properties)
Fetched Properties are used to let SharePoint know which metadata should be retrieved for each result.
The column that needs to be added is: “SiteTitle”.
To add these columns go to your search result page or the page where you are using the core search web part.
Turn the page in edit mode;
Edit the “Search Core Results” web part;
Open the “Display Properties” section;
Display Properties Section
Add the following column to the “Fetched Properties”;
Click “Ok”.
The full “Fetched Properties” XML is the following:
<xsl:templatename="SPWebUrl"><xsl:paramname="siteUrl"/><xsl:paramname="contentclass"/><xsl:choose><!-- Check the content class to see if it is a document --><xsl:whentest="$contentclass='STS_ListItem_DocumentLibrary'"><!-- Get Document Library Url Name --><xsl:variablename="DocLib"><xsl:call-templatename="StripSlash"><xsl:with-paramname="text"select="$siteUrl"/></xsl:call-template></xsl:variable><!-- Remove the document library from the url --><xsl:variablename="SPWebURLString"select="substring-before(concat($siteUrl, '/'), concat('/', concat($DocLib, '/')))"/><xsl:value-ofselect="$SPWebURLString"/></xsl:when><xsl:otherwise><!-- Get List Url Name --><xsl:variablename="ListUrl"><xsl:call-templatename="StripSlash"><xsl:with-paramname="text"select="$siteUrl"/></xsl:call-template></xsl:variable><!-- Remove the list name from the url --><xsl:variablename="urlLists"select="substring-before(concat($siteUrl, '/'), concat('/', concat($ListUrl, '/')))"/><!-- Remove Lists from the url --><xsl:variablename="Lists"><xsl:call-templatename="StripSlash"><xsl:with-paramname="text"select="$urlLists"/></xsl:call-template></xsl:variable><xsl:variablename="SPWebURLString"select="substring-before(concat($urlLists, '/'), concat('/', concat($Lists, '/')))"/><xsl:value-ofselect="$SPWebURLString"/></xsl:otherwise></xsl:choose></xsl:template><xsl:templatename="StripSlash"><xsl:paramname="text"/><xsl:choose><xsl:whentest="contains($text, '/')"><xsl:call-templatename="StripSlash"><xsl:with-paramname="text"select="substring-after($text, '/')"/></xsl:call-template></xsl:when><xsl:otherwise><xsl:value-ofselect="$text"/></xsl:otherwise></xsl:choose></xsl:template>
The next step is to add the following piece of code somewhere inside the “Results” template.
Each search result contains the site hyperlink (depending on the location in the result template).
Final Result
This solution only works on items and documents. When sites are returned as results, the site url gets broken. To fix this you could check if the result “contentclass” is not equal to “STS_Site”.