Export (0) Print
Expand All
Expand Minimize
3 out of 7 rated this helpful - Rate this topic

Creating Search Queries Programmatically by using the Search Object Model in SharePoint Server 2007

SharePoint 2007

Summary:  Learn how to programmatically create search queries by using the Microsoft Office SharePoint Server 2007 object model.

Office Visual How To

Applies to:  Microsoft Office SharePoint Server 2007

Patrick Tisseghem, U2U

July 2007

Overview

You can execute search queries programmatically by either working directly with the object model within the context of Microsoft Office SharePoint, or when working remotely, by using the Search Web service. The Office SharePoint Server 2007 object model offers a new set of classes you can use to program the execution of a search query.

Code It

For the following code to work, you need a reference to the Microsoft.Office.Server.Search.dll. The main namespace used within the sample code is Microsoft.Office.Server.Search.Query.

Constructing a Keyword Query String

Using the keyword syntax to construct the query string is straightforward. To construct the query string, prefix the keywords you want to exclude from the search results with a hyphen (-) character; prefix the keywords you want to include in the search results with a plus (+) sign. You can also add property criteria. Following is a query that when executed returns all documents containing the keyword "Business" but excluding the documents written by "Brian".

Business -isDocument:0 -author:brianc

Constructing a FullText Query String

A query string formulated by using an extension of the SQL language allows a more powerful search request that supports full-text search and more control on the returned fields, and that allows the use of search scopes and properties in conditions. Following is the previous query formulated by using the FullText query string.

SELECT Title, Rank, Write, Url FROM SCOPE() 
   WHERE 'Scope'= 'Marketing Documents' AND
   FREETEXT('business') AND isDocument 1 0
   AND author IS NOT 'Brian'}

Executing a Keyword Query String

You execute the query string you formulate with the keyword syntax by creating an instance of an object of the KeywordQuery class, and then by providing it with the context of the Shared Services Provider (SSP) within the server farm. You can do this with an instance of the SPSite object. Next, you typically set one or more properties to configure the execution and assignment of the query string for the QueryText property. The Execute method returns a ResultTableCollection object that contains the search results. By using the ResultType enum, you have the option to filter the type of results you want and possibly bind them to a Windows–based or ASP.NET control.

SPSite site = new 
   SPSite("http://moss.litwareinc.com");
KeywordQuery kwq = new KeywordQuery(site);
kwq.QueryText = textBoxKeyword.Text;
kwq.ResultTypes =
   ResultType.RelevantResults;
ResultTableCollection results =
   kwq.Execute();
ResultTable resultTable =
   results[ResultType.RelevantResults];

Executing a FullText Query String

A query string formulated with the FullText query language follows the same flow of execution as the previous code example. However, now we use the FullTextSqlQuery class. The following example code shows another way to provide the SSP context in the constructor. This time, a reference to Microsoft.Office.Server.ServerContext does the job.

ServerContext context =
ServerContext.GetContext("SharedServices1");
FullTextSqlQuery qry = new
   FullTextSqlQuery(context);
qry.ResultTypes = 
   ResultType.RelevantResults;
qry.EnableStemming = true;
qry.TrimDuplicates = true;
qry.QueryText = query;
ResultTableCollection results =
   qry.Execute();
ResultTable resultTable =
   results[ResultType.RelevantResults];
Read It

Microsoft Office SharePoint Portal Server 2003 contained a class named QueryProvider to execute search queries programmatically. This class is still available, but is now deprecated. Office SharePoint Server 2007 offers two new classes; both inherit from the Query base class:

  • KeywordQuery executes a search query constructed by using the keyword syntax.

  • FullTextSqlQuery executes search queries constructed by using the FullText SQL syntax.

The flow of execution is the same for both.

See It

Creating Search Queries using OM video

Watch the Video

Length: 12:47 | Size: 8.85 MB | Type: WMV file

Explore It
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.