In SharePoint 2010, you can use the query object model to build custom search
Web Parts and search applications. This object model allows you to query against SharePoint Server search as well as to FAST Search Server 2010 for SharePoint.
The FullTextSqlQuery class available in the object model allows you to build complex search queries based on SQL syntax however, the KeywordQuery class can be used to issue search queries based on simple query syntax. With Fast Search Server 2010, this class can also be used to run queries based on the Fast Query Language (FQL), which supports advanced queries in enterprise environments.
The example below demonstrates using the query object model to issue a search query based on a keyword search with the KeywordQuery class. This example is built as an application page with a text input field called txtSearch, a button for submitting the query called btnSearch, and an SPGridView called searchGrid.
Please Note : Before you can run search queries, you also need to specify the search server that should be used for indexing in Central Administration, under Application Management -> Databases -> Manage Content Databases -> [Your content DB] > Search Server.
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Query;
namespace Apress.SP2010.Layouts.CustomSearch
{
public partial class ApplicationPage1 : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetGridColumns();
}
}
// The event is called when the search button is pressed
protected void btnSearch_Click(object sender, EventArgs e)
{
DataTable search = new DataTable();
using (SPSite site = new SPSite(SPContext.Current.Web.Site.Url))
{
KeywordQuery query = new KeywordQuery(site);
query.ResultsProvider = Microsoft.Office.Server.Search.Query.
SearchProvider.Default;
query.ResultTypes = ResultType.RelevantResults;
query.KeywordInclusion = KeywordInclusion.AllKeywords;
query.QueryText = txtSearch.Text;
ResultTableCollection results = query.Execute();
if (results.Count > 0)
{
ResultTable relevant = results[ResultType.RelevantResults];
search.Load(relevant);
DataView view = new DataView(search);
// Fill the SPGridView defined in the page markup
searchGrid.DataSource = search;
searchGrid.DataBind();
}}}
private void SetGridColumns()
{
SPBoundField fieldTitle = new SPBoundField();
fieldTitle.HeaderText = "Title";
fieldTitle.DataField = "Title";
SearchGrid.Columns.Add(fieldTitle);
SPBoundField fieldPath = new SPBoundField();
fieldPath.HeaderText = "Path";
fieldPath.DataField = "Path";
SearchGrid.Columns.Add(fieldPath);
SPBoundField fieldAuthor = new SPBoundField();
fieldAuthor.HeaderText = "Edited";
fieldAuthor.DataField = "Write";
SearchGrid.Columns.Add(fieldAuthor);
}}}
In the above code, the OnClick handler for the button creates a KeywordQuery object and passes the text from the text field to the QueryText property. After calling Execute, the results are available through the ResultTable at the index ResultType.RelevantResults in the returned ResultTableCollection. Using a DataTable, these results are bound to the SearchGrid for display. The relevant table columns are bound using the SetGridColumns method during Page_Load.