in SharePoint 2007

Using KeywordQuery to search in your SharePoint site

There are various ways that you can query the Search Service in your SharePoint site:

1) Using the Search Web Service

2) Using the SharePoint Object Model

There are yet again two ways you can use the Search, using the Object Model:

i) KeywordQuery

ii) FullTextSqlQuery

I think it is a good option to use Object Model when you want to customize the Search (mostly in your WCM websites), and KeywordQuery comes in very handy!

Using KeywordQuery to search is pretty simple! (provided you have set up your Search properly)

KeywordQuery keywordQuery = new KeywordQuery(SPContext.Current.Site);

keywordQuery.ResultTypes = ResultType.RelevantResults | ResultType.SpecialTermResults;

keywordQuery.StartRow = 1;

keywordQuery.RowLimit = 5;

keywordQuery.SortList.Add("Rank", SortDirection.Descending);

keywordQuery.EnableStemming = true;

keywordQuery.TrimDuplicates = true;

keywordQuery.IgnoreAllNoiseQuery = true;

keywordQuery.HiddenConstraints = "scope:" + """ + SearchScope + """;

keywordQuery.QueryText = SearchText;

ResultTableCollection searchResults = keywordQuery.Execute();

As you can see above, I have combined the result type to be both RelevantResults and SpecialTermResults

Note: In WSS, only RelevantResults will work

One other thing to note in the above code is how we specify the scope using the HiddenConstraints

The search results are returned as a table collection. You can access the search tables as follows:

ResultTable relevantResultsTable = searchResults[ResultType.RelevantResults];

ResultTable specialTermResultsTable = searchResults[ResultType.SpecialTermResults];

If you want to load these into a DataSet with tables RelevantResults and SpecialTermResults respectively, then:

// Relevant Results table

DataTable relevantResults = new DataTable();

relevantResults.TableName = "RelevantResults";


// SpecialTerms Results table

DataTable specialTermResults = new DataTable();

specialTermResults.TableName = "SpecialTermResults";


// Search Results DataSet

DataSet resultSet = new DataSet();


 // Relevant Results

relevantResults.Load(relevantResultsTable, LoadOption.OverwriteChanges);



// SpecialTerms Results


specialTermResults.Load(specialResultsTable, LoadOption.OverwriteChanges);

You can now easily databind this DataSet



If you have worked with the Search Web Service, you will notice that the properties in KeywordQuery and the Search Query XML are similar

<?xml version="1.0" encoding="utf-8" ?>

<QueryPacket xmlns="urn:Microsoft.Search.Query" Revision="1000">

<Query domain="QDomain">



  <QueryText language="en-US" type="STRING" >  SCOPE:"All Sites"</QueryText>


<SortByProperties><SortByProperty name="Rank" direction="Descending" order="1"/></SortByProperties>










(Above Search Query XML was generated using SharePoint Search Service Tool)

Write a Comment


Time limit is exhausted. Please reload the CAPTCHA.

  1. Hi,

    Can we also specify other managed properties in HiddenConstraints? Suppose I want to search for keywords in a particular scope where color=red.

  2. HI

    I want to search the value with "Contains" ,"And" "Where Condition" statement.

    Can u suggest how to write using KeyWordQuery in sharepoint 2007

    Thanks & Regards