How to Display the Site Title and Site URL in Search Results

August 3, 2011

This post is over a year old, some of this information may be out of date.

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
Search Result with Site Reference
The things I did to establish this were:

  1. Adding Site Title column references to the fetched properties of the Core Result Web Part.
  2. 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
    Display Properties Section
  • Add the following column to the “Fetched Properties”;
  • Click “Ok”. The full “Fetched Properties” XML is the following:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<Columns>
  <Column Name="WorkId"/>
  <Column Name="Rank"/>
  <Column Name="Title"/>
  <Column Name="Author"/>
  <Column Name="Size"/>
  <Column Name="Path"/>
  <Column Name="Description"/>
  <Column Name="Write"/>
  <Column Name="SiteName"/>
  <Column Name="CollapsingStatus"/>
  <Column Name="HitHighlightedSummary"/>
  <Column Name="HitHighlightedProperties"/>
  <Column Name="ContentClass"/>
  <Column Name="IsDocument"/>
  <Column Name="ServerRedirectedURL"/>
  <Column Name="SiteTitle"/>
</Columns>

Search result XSL styling

Now that the” SiteTitle” column is added as to the Fetched Properties, you can edit the Search result style.

  • Edit the “Search Core Results” web part;
  • Under the “Display Properties” section, click on the “XSL Editor” button;
    XSL Editor
    XSL Editor
  • Copy the content of the XSL Editor to the text editor of your choice;
  • Add the following two XSL templates before the /xsl:stylesheet closing tag;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<xsl:template name="SPWebUrl">
  <xsl:param name="siteUrl" />
  <xsl:param name="contentclass" />

  <xsl:choose>
    <!-- Check the content class to see if it is a document -->
    <xsl:when test="$contentclass='STS_ListItem_DocumentLibrary'">
      <!-- Get Document Library Url Name -->
      <xsl:variable name="DocLib">
        <xsl:call-template name="StripSlash">
          <xsl:with-param name="text" select="$siteUrl"/>
        </xsl:call-template>
      </xsl:variable>

      <!-- Remove the document library from the url -->
      <xsl:variable name="SPWebURLString" select="substring-before(concat($siteUrl, '/'), concat('/', concat($DocLib, '/')))" />

      <xsl:value-of select="$SPWebURLString"/>
    </xsl:when>
    <xsl:otherwise>
      <!-- Get List Url Name -->
      <xsl:variable name="ListUrl">
        <xsl:call-template name="StripSlash">
          <xsl:with-param name="text" select="$siteUrl"/>
        </xsl:call-template>
      </xsl:variable>
      <!-- Remove the list name from the url -->
      <xsl:variable name="urlLists" select="substring-before(concat($siteUrl, '/'), concat('/', concat($ListUrl, '/')))" />
      <!-- Remove Lists from the url -->
      <xsl:variable name="Lists">
        <xsl:call-template name="StripSlash">
          <xsl:with-param name="text" select="$urlLists"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:variable name="SPWebURLString" select="substring-before(concat($urlLists, '/'), concat('/', concat($Lists, '/')))" />

      <xsl:value-of select="$SPWebURLString"/>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="StripSlash">
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="contains($text, '/')">
      <xsl:call-template name="StripSlash">
        <xsl:with-param name="text" select="substring-after($text, '/')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
  • The next step is to add the following piece of code somewhere inside the “Results” template.
1
2
3
4
5
6
7
8
9
<a>
  <xsl:attribute name="href">
    <xsl:call-template name="SPWebUrl">
      <xsl:with-param name="siteUrl" select="sitename" />
      <xsl:with-param name="contentclass" select="contentclass" />
    </xsl:call-template>
  </xsl:attribute>
  <xsl:value-of select="sitetitle" />
</a>
  • Click “OK” and save the page.

Result

Each search result contains the site hyperlink (depending on the location in the result template).

Final Result
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”.

Comments

comments powered by Disqus