First, you have to add the following references:

  • Microsoft.Sharepoint
  • Microsoft.Sharepoint.Search

Second:

DataTable resultsDataTable = new DataTable();

 

using (SPSite site = new SPSite("http://server"))

{

 

KeywordQuery query = new KeywordQuery(site);

query.QueryText = queryText;

query.ResultTypes = ResultType.RelevantResults;

 

ResultTableCollection resultTables = query.Execute();


if (resultTables.Count > 0)

{

ResultTable relevantResults = resultTables[ResultType.RelevantResults];

 

resultsDataTable.Load(relevantResults, LoadOption.OverwriteChanges);

}

 

}

 

return resultsDataTable;

 

In WSS the only ResultType possible is RelevantResults but you have to define it with the following statement:

query.ResultTypes = ResultType.RelevantResults;

 

If you don't do that the ResultTableCollection doesn't return any ResultTable.

The properties returned are:

  • Work ID
  • Rank
  • Title
  • Author
  • Size
  • Path
  • Description
  • Write
  • SiteName
  • CollapsingStatus
  • HitHighlightedSummary
  • HitHighlightedProperties
  • ContentClass
  • IsDocument
  • PictureThumbnailURL

If you want you can control exactly what properties are returned by adding properties to the StringCollection – query.SelectProperties

 


 

query.SelectProperties.Add("title");

query.SelectProperties.Add("size");

 

 

In the previous example only the Title and the Size are returned.

If you want you can page the results:

int pageSize = 3;

int page = 0;

 


 

query.StartRow = page * pageSize;

query.RowLimit = pageSize;

 

You can sort the results by adding items to the SortCollection list.


 

query.SortList.Add("Rank", SortDirection.Ascending);

query.SortList.Add("WorkId", SortDirection.Descending);

I don't know why but it seems that only the first sort item works. I tried to change the direction and also the property of the second sort item but the results were always ordered the same way.

LEAVE A REPLY

Please enter your comment!
Please enter your name here