SharePoint: Add a Filter in the Refinement Panel of the Search Page


You may also be interested in: SharePoint Smart Notifications by KWizCom


 

Editor’s note: Contributor Dhaval Shah is an Analyst at Deloitte. Follow him @beingdhavalshah

Extract XML of the Filter Category Definition

We need to extract the xml of the Filter Category Definition from the existing default refinement panel.

  1. In your browser, navigate to the results page URL:
  2. http://SiteURL/Pages/results.aspx

  3. On the Site Actions menu, click Edit Page.
  4. In the Refinement Panel Web Part, click the edit arrow to display the Web Part menu, and then click Edit Web Part. This opens the Web Part tool pane.
  5. In the Refinement section, clear the Use Default Configuration check box.
  6. 2012-05-15-RefinementPanel-01.png

  7. In the Refinement section, click the ellipsis (…) button for the Filter Category Definition field.

This opens the Text Entry window for the Refinement Web Part’s FilterCategoriesDefinition property. You can modify the XML directly in this window; however, you may find it easier to copy the code to a file.

2012-05-15-RefinementPanel-02.png

Edit the XML in Visual Studio

Now we need to edit the given xml, preferably, in Visual Studio. You will find a filter is already present for the Result Type, Site, Author,etc by default. We will try to add our own filter to it.

We will add ‘Content Source’ category filter to it, so that we can filter based on the content source from which the result was crawled.

Scroll to the end of the xml and add the following line before </FilterCategories> tag


<Category    Title="Content Source"    Description="Use this filter to restrict results authored by a specific content source"    Type="Microsoft.Office.Server.Search.WebControls.ManagedPropertyFilterGenerator"    MetadataThreshold="1"    NumberOfFiltersToDisplay="4"    MaxNumberOfFilters="20"    ShowMoreLink="True"    MappedProperty="contentsource" SortBy="Frequency"    SortDirection="Descending"    SortByForMoreFilters="Name"    SortDirectionForMoreFilters="Ascending"   MoreLinkText="show more"    LessLinkText="show fewer"    ShowCounts="Count" />

Save back the edited XMl of FilterCategoriesDefinition in webpart
Copy back the edited xml to the webpart’s FilterCategoriesDefinition property and click ok. Now our webpart is ready to use content source as a filter. But the catch here is that thecontent source property is not retrieved in the ContentCoreResult Webpart without which our filter will be of no use.

Add the column for Content Source
Add the column for “Content Source” in the ContentCoreResult webpart

  1. In your browser, navigate to the results page URL:
  2. http://SiteURL/Pages/results.aspx

  3. On the Site Actions menu, click Edit Page.
  4. In the Content Core Result Web Part, click the edit arrow to display the Web Part menu, and then click Edit Web Part. This opens the Web Part tool pane.
  5. In the Core Results->Display Properties section, clear the Use Default Configuration check box.
  6. In the Display Properties section, Copy the fetched Properties in the Visual Studio editor
  7. Now add the following line at the end of the document before </Columns> tag and save the webpart properties

<Column Name="contentsource"/>

Make sure there is an entry for the managed property content source mapped to the crawled property Content Source.

2012-05-15-RefinementPanel-03.png

Result

Now go to the search site and search for the result, you will be able to see new filter added to the refinement panel.

2012-05-15-RefinementPanel-04.png