Exercise 4: Extending Search Web Parts

In SharePoint 2010, all of the search web parts may be extended through inheritance. This allows you to interact with the query pipeline both before and after the query is run. In this exercise, you’ll create a web part that makes use of your custom ranking model.

  1. Start Visual Studio 2010 and select File » New » Project from the main menu.
  2. In the New Project dialog, select VisualC#/Visual Basic » SharePoint » 2010 » Empty SharePoint Project. Give it the name SearchCustomizations.
  3. On the SharePoint Customization Wizard dialog box choose Deploy as a farm solution to the http://intranet.contoso.com/sites/Lab11 site and click Finish.
  4. When the new project is created, right click the project and select Add » New Item. When prompted, pick the template Web Part and give it the name CoreResultsWithRankingModelID.
  5. Select Project » Add Reference… from the Visual Studio main menu.
  6. In the Add Reference dialog, on the .NET tab select the first Microsoft ® Search component. Alternatively, if you have trouble finding this, browse to c:\Program Files\Common Files\Microsoft Shared\web server extensions\14\ISAPI and select Microsoft.Office.Server.Search.dll. Then click the OK button.
  7. Open the Web Part in code view and add the following statements to the top of the code window.

    C#

    using System; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.Office.Server.Search.WebControls; using Microsoft.Office.Server.Search.Query; using System.Xml.XPath;

    VB.NET

    Imports System Imports System.ComponentModel Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports Microsoft.SharePoint Imports Microsoft.SharePoint.WebControls Imports Microsoft.Office.Server.Search.WebControls Imports Microsoft.Office.Server.Search.Query Imports System.Xml.XPath
  8. Now in the same code view add the following code

    C#

    [ToolboxItemAttribute(false)] public class CoreResultsWithRankingModelID : CoreResultsWebPart { QueryManager qm; protected override XPathNavigator GetXPathNavigator(string viewPath) { try { qm = SharedQueryManager.GetInstance(this.Page).QueryManager; qm.UserQuery = " scope:Important Tasks"; foreach (LocationList ll in qm) { foreach (Location l in ll) { try { RankingModelID ="c978ef2b-300a-444b-af9a-d51261294587"; } catch { } } } } catch { } return base.GetXPathNavigator(viewPath); } }

    <ToolboxItemAttribute(false)> _ Public Class CoreResultsWithRankingModelID Inherits CoreResultsWebPart Private qm As QueryManager Protected Overrides Function GetXPathNavigator(ByVal viewPath As String) _ As XPathNavigator Try qm = SharedQueryManager.GetInstance(Me.Page).QueryManager qm.UserQuery = " scope:Important Tasks" For Each ll As LocationList In qm For Each l As Location In ll Try l.RankingModelID = "c978ef2b-300a-444b-af9a-d51261294587" Catch End Try Next Next Catch End Try Return MyBase.GetXPathNavigator(viewPath) End Function End Class
  9. Build the project and verify that the code compiles. If everything builds, Deploy the project.
  10. In the Search Center at http://intranet. Search Centercontoso.com/sites/Lab11/searchcenter, select the Important Tasks tab and enter some text and click the Search button (i.e. magnifying glass button) to navigate to the ImportantTasksResults.aspx page.
  11. Put this page in edit mode by selecting Site Actions » Edit Page.
  12. Delete the Search Core Results web part from the Bottom Zone.
  13. Insert the new Web Part by clicking on the Bottom Zone on the page and the Insert group on the ribbon should appear. Now on the Insert ribbon group click the Web Part button.
  14. Your new Web Part will be in the Custom category. Locate the part and then click the Add button.
  15. In the CoreResultsWithRankingModelID web part select the Web Part Menu drop down arrow (top right corner of web part) and select Edit Web Part.
  16. On the Edit properties window, expand Location Properties
  17. Try running a query and verifying that the web part is returning results.
    Note:
    In this exercise you created a custom Web Part that inherited and extended the functionality of the Search Core Results Web Part.