-->

Wednesday 18 January 2012

Customizing CoreResultWebpart in FAST Search Center

Create an empty sharepoint project
Create an empty Sharepoint Project and name it as a 'MyCustomCoreResult'
Customizing CoreResultWebpart in FAST Search Center

Click Ok and then enter the url of the sharepoit fast search site
Customizing CoreResultWebpart in FAST Search Center 


Add Custom Core Results Webpart in the project (inherit from CoreResultsWebpart)
Now right click on the project and click on add -> 'New item' ->  Webpart
Customizing CoreResultWebpart in FAST Search Center

Enter the name of webpart as MyCustomCoreResultWebpart and click on Add button

If you look at the Code behind file of the webpart i.e. MyCustomCoreResultWebpart.cs, by default it would be inherited from the Webpart class replace it with the CoreResultsWebPart class.

Note : You might have to add the Microsoft.Office.Server.Search.dll most likely located under C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Office.Server.Search.dll

Overriding the methods
You need to override following 3 methods
a.       CreateChildControls
protected override void CreateChildControls()
{
  base.CreateChildControls();
}

b.      OnInit
protected override void OnInit(EventArgs e)
{
 base.OnInit(e);
}
c.       ChangeDataSourceProperties
protected override void ConfigureDataSourceProperties()
{
  base.ConfigureDataSourceProperties();
  this.XslLink = "/_layouts/xsl/MyCustomCoreResultsXSLT.xslt";
}

Add a SharePoint Mapped Folder

Now we need to add our custom XSLT file to the sharepoint site. Right click on the project and click on Add->Sharepoint Mapped Folder.
Customizing CoreResultWebpart in FAST Search Center


Now select the appropriate folder for you custom XSLT to reside. For our example I have selected the XSL folder under {SharepointRoot} -> Template -> Layouts -> XSL folder. Click on Ok and it will map the sharepoint XSL folder to our project.

Add CustomCoreResult.xslt file
Now we need to get the default xslt of the coreresultwebpart. You can get it from here
Get the content of the xslt from the above link and paste it in this file. You can customize the look and feel of the webpart using this xslt. For this example we will just highlight the matched string of the userquery with orange color.
Search for the string “c0” in the file, You will find something like this

<xsl:template match="c0">
    <b>
      <xsl:value-of select="."/>
    <b>
</xsl:template>


Replace the above section with the following one.

<xsl:template match="c0">
    <span style="background-color:orange">
      <b>
        <xsl:value-of select="."/&gt;
      </b>
    </span>
</xsl:template>
    
    Build and Deploy Project
This will automatically deploy the webpart and feature in the site you selected earlier. By default feature will be activated. In case it’s not activated, you might have to activate it manually.
     Open the site and edit the Results.aspx page to replace the default coreresults webpart with our custom webpart from the custom category in webpart gallery.
      Save the page and now the results.aspx should be using your webpart.
Customizing CoreResultWebpart in FAST Search Center




As you can see now the highlighted matches will appear in orange color. Some of the other things that you can try is adding new columns in the display results. I have added the Site Name, Company, Content Source details in the search results.


1 comment: