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()

b.      OnInit
protected override void OnInit(EventArgs e)
c.       ChangeDataSourceProperties
protected override void 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">
      <xsl:value-of select="."/>

Replace the above section with the following one.

<xsl:template match="c0">
    <span style="background-color:orange">
        <xsl:value-of select="."/&gt;
    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.

Thursday, 12 January 2012

Get XML data from the CoreResultsWebPart

We often want to debug the XSLT code that we have written for our CoreResultsWebPart, this post will help you to extract the data from the CoreResultsWebPart and later which we can apply to debug our custom XSLT written for the CoreResultsWebPart.


1. You need to have the Administrator privileges
2. Basic understanding of creating web applications and running the FAST search.


First thing you need to do is get the Basic Fast Search Center site up and running. Below i have mentioned the brief steps.
1. Create Web application with classic mode authentication.
2. Create a root site collection with "Fast Search Center" template.
3. Open the root site collection and try to search, as long as it displays the result you are fine :)

First Step : Edit the page and replace the default XSL with our custom XSL

As you Site is up and running, search for any term and go to the results page. Edit the page from Site Actions -> Edit Page
Search for the Search Core Results web part on the page and edit the web part. In the search core results editor pane, go to the Core Results->Display properties.

Uncheck the "Use Location Visualization" option, by default it would be checked.
Note: It means that site has to use the columns and XSL defined at the federated location level. Which is shared by all the search sites in the farm. When we uncheck this option we can define our own XSL format. More information on this topic can be found here
On unchecking this option the area under Fetched Properties and XSL editor will be enabled.
Fetched Properties are the columns that you want to be retrieved from the FAST search database. The columns mentioned within these properties will only be fetched.

Now click on the XSL editor button, it will open the dialog displaying the XSL used for the CoreResultsWebpart. Replace the default XSLT with the following code. Don't forget to take the backup of default code in case you want to revert back to original code :)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="*"/>

 Click on Ok and save the page. 

Second Step : View the Search Results Web part XML data

Right-click the page zone that contains the Search Results Web Part that you want to view the search results XML data for, and then click View Source on the pop-up menu. This opens a page in Notepad that contains the HTML source. 

Search for the string “<All_Results>”. This is the opening tag for the section that contains the full search results set for the Web Part you modified.