You can use the query object model to write custom web parts that consume data from search. At the heart of the query object model are the following two classes.
• FullTextSqlQuery—Enables you to execute SQL syntax search queries
• KeywordQuery—Enables you to execute Keyword syntax search queries
Try It Yourself: Create a Custom Search Web Part
In this section you create a custom web part that uses the FullTextSqlQuery
to display data from the Technical Resource scope where the TechnicalKeywords field contains .Net. Follow these steps:
1. Configure the TechnicalKeywords field as a managed property since you will be querying on that field. Browse to the Search Service Application and go to the Metadata Properties screen.
2. Click New Managed Property link on the top menu.
3. In the New Managed Property screen enter the Property name as TechnicalKeyword and click the Add Mapping button. Select the ows_TechnicalKeywords(Text) crawled property and click OK.
4. Leave the rest of the fields set to their default values and click OK.
5. Perform a full crawl.
6. Create a new Empty SharePoint project named SearchDemo in Visual Studio 2010.
7. Add a visual web part named TechnicalResourceDisplayWebPart.
8. Add a reference to Microsoft.Office.Server.dll and Microsoft.Office.Server.Search.dll.
9. Update the code of TechnicalResourceDisplayWebPartUserControl.ascx as shown in the following code:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint"
Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities"
Namespace="Microsoft.SharePoint.Utilities"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp"
Namespace="System.Web.UI"
Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages"
Namespace="Microsoft.SharePoint.WebPartPages"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#"
AutoEventWireup="true"
CodeBehind="TechnicalResourceDisplayWebPartUserControl.ascx.cs"
Inherits="SearchDemo.TechnicalResourceDisplayWebPart.TechnicalResourceDisplayWebPartUserControl" %>
<asp:DataList runat="server" ID="dataList1">
<ItemTemplate>
<asp:HyperLink NavigateUrl='<%# Eval("Path") %>'
Text='<%# Eval("Title") %>'
runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:DataList>
10. Update the code of TechnicalResourceDisplayWebPartUserControl.ascx.cs as follows:
using System;
using System.Data;
using System.Web.UI;
using Microsoft.Office.Server.Search.Query;
using Microsoft.SharePoint;
namespace SearchDemo.TechnicalResourceDisplayWebPart
{
public partial class
TechnicalResourceDisplayWebPartUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
GetTechnicalResource();
}
protected void GetTechnicalResource()
{
using (SPSite site = new SPSite("http://splearn"))
{
FullTextSqlQuery myQuery = new FullTextSqlQuery(site);
myQuery.QueryText = @"
SELECT
Title,
Path,
TechnicalKeywords
FROM SCOPE()
WHERE ""Scope""='Technical Resource' AND Contains(TechnicalKeywords,'.NET')";
myQuery.ResultTypes = ResultType.RelevantResults;
// execute the query and load the results into a datatable
myQuery.RowLimit = 100;
myQuery.TrimDuplicates = false;
ResultTableCollection results = myQuery.Execute();
ResultTable resulttable =
results[ResultType.RelevantResults];
DataTable data = new DataTable();
data.Load(resulttable, LoadOption.OverwriteChanges);
dataList1.DataSource = data;
dataList1.DataBind();
}
}
}
}
11. Deploy the solution and add the TechnicalResourceDisplayWebPart to the home page of your site. You should be able to see data in the web part if the data is crawled, and the word “.NET” is present in the TechnicalKeywords field as shown in Figure 18.24. You can navigate to the individual items by clicking on the links.