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.
- Start Visual Studio 2010 and select File » New » Project from the main menu.
- In the New Project dialog, select VisualC#/Visual Basic » SharePoint » 2010 » Empty SharePoint Project. Give it the name SearchCustomizations.
- On the SharePoint Customization Wizard dialog box choose Deploy as a farm solution to the https://intranet.contoso.com/sites/Lab11 site and click Finish.
- 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.
- Select Project » Add Reference… from the Visual Studio main menu.
- 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.
- Open the Web Part in code view and add the following statements to the top of the code window.
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;
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
Now in the same code view add the following code
[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
- Build the project and verify that the code compiles. If everything builds, Deploy the project.
- In the Search Center at https://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.
- Put this page in edit mode by selecting Site Actions » Edit Page.
- Delete the Search Core Results web part from the Bottom Zone.
- 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.
- Your new Web Part will be in the Custom category. Locate the part and then click the Add button.
- In the CoreResultsWithRankingModelID web part select the Web Part Menu drop down arrow (top right corner of web part) and select Edit Web Part.
- On the Edit properties window, expand Location Properties
- Try running a query and verifying that the web part is returning results.
In this exercise you created a custom Web Part that inherited and extended the functionality of the Search Core Results Web Part.