Programmatically Setting the Fetched Properties to an Extended Search Core Result Web Part

Search Search Center Web Part
post

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

This week I needed to extend the search core result web part to be able to do some custom filtering and search result styling.

For the search result styling, additional fetched properties (columns) needed to be added programmatically to the Web Part properties. But every time I checked in the web part properties, these fetched properties were not added, and I end up with the default fetched properties.

After investigating the “Microsoft.Office.Server.Search” assembly I saw that you must meet three conditions.

  1. The “SelectColumns” (Fetched Properties) string may not be Null or empty;
  2. The “PropertiesToRetrieve” must be Null or Empty;
  3. “UseLocationVisualization” must be false.
set
{
if ((!string.IsNullOrEmpty(value) && string.IsNullOrEmpty(base.PropertiesToRetrieve)) && !base.UseLocationVisualization)
{
base.PropertiesToRetrieve = value;
}
}

Solution

The solution to programmatically add your columns to the fetched properties is the following:

protected override void OnInit(EventArgs e)
{
//Set an empty string for the PropertiesToRetrieve property
base.PropertiesToRetrieve = string.Empty;
//Set the UseLocationVisualization property to false
base.UseLocationVisualization = false;
//Set the fetched properties
base.SelectColumns = fetchProp;
}

“fetchProp” is a string that contains the following content.

<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="SiteTitle"/>
</Columns>

Want the next article?

Subscribe to the RSS feed, or follow me where I share new posts.

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

I build developer tools · Microsoft 365 architect · Speaker

Loading...

Let's build together

All your AI coding agents in one window

Present from VS Code

Manage content in VS Code

See all my projects →