Tuesday, December 13, 2011

How to display source site title in Content Query Web Part and Search Results?


How to display source site title in Content Query Web Part and Search Results?


It is a not so uncommon requirement to group documents / list items by sites and display the site title for each group.

Content Query Web Part returns lot of data but site title is not returned by it by default.

SharePoint search service application does not come with property for site name or site title by default but luckily sharepoint search crawls and indexes site name as ows_sitename



In the .webpart file there is a property called ViewFieldsOverride. This property overrides the fields that are loaded by the CQWP. This gives us an opportunity to add the Listname when querying the data.

Since we are overriding it, we first need to add the default fields (Title, Created etc) manually. After that, we add the listname and sitename using the <ListProperty Name="Title" /> and <ProjectProperty Name="Title" />

<property name="ViewFieldsOverride" type="string"><![CDATA[<FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Nullable="True" Type="Text" /><FieldRef ID="{94f89715-e097-4e8b-ba79-ea02aa8b7adb}" Nullable="True" Type="Lookup" /><FieldRef ID="{1d22ea11-1e32-424e-89ab-9fedbadb6ce1}" Nullable="True" Type="Counter" /><FieldRef ID="{28cf69c5-fa48-462a-b5cd-27b6f9d2bd5f}" Nullable="True" Type="DateTime" /><FieldRef ID="{1df5e554-ec7e-46a6-901d-d85a3881cb18}" Nullable="True" Type="User" /><FieldRef ID="{d31655d1-1d5b-4511-95a1-7a09e9b75bf2}" Nullable="True" Type="User" /><FieldRef ID="{8c06beca-0777-48f7-91c7-6da68bc07b69}" Nullable="True" Type="DateTime" /><FieldRef Name="PublishingRollupImage" Nullable="True" Type="Image" /><FieldRef Name="_Level" Nullable="True" Type="Number" /><FieldRef Name="Comments" Nullable="True" Type="Note" /><ListProperty Name="Title" /><ProjectProperty Name="Title" />]]></property>

Now it’s simply a matter of displaying the property in my custom XSL using: 
<xsl:value-of select=”@ProjectProperty.Title”/>

Search Results:

There is no metadata property that comes OOTB for site title by the Search Service Application. Site name is being crawled and indexed as ows_sitename.

To make this property available in search results, simply create a new Managed Property called "SiteTitle" and map it to ows_sitename.

In search core results web part's fetched properties, add a new column using 
<Column Name=”SiteTitle”/> and use it to display in your custom XSLT.


No comments:

Post a Comment