Searching Custom columns in SharePoint

The SharePoint Keywordquery and Fulltextsearch are good ways to search metadata programmatically. This option is only available for MOSS 2007 because you don’t have a server context object in WSS 3.0.

When you Create a keyword query instance the metadata properties that will be returned will only be the basic SharePoint metadata fields like name, title etc. When you want to get your own created columns in the results you’ll have to add the property names to the to the SelectProperties object from the keywordquery.

// Set result properties

To search custom SharePoint properties you’ll have to set these properties as Managed Metadata first. After you’ve done this it’s pretty easy. You can just put “propertyname:value” as QueryText and then the search will search the property “propertyname” which contain “value”. You’ll have to put quotes around the search value because otherwise the search will also show results where the value is only a part of the property value.

Below is a full example how to create a KeywordQuery in code for in example your custom webparts.

string SSP = ConfigurationManager.AppSettings["SSP"].ToString(); 
ServerContext context = ServerContext.GetContext(SSP);
KeywordQuery kwq = new KeywordQuery(context);

// Set properties for the query before executing
kwq.ResultTypes = ResultType.RelevantResults;
kwq.EnableStemming = true;
kwq.TrimDuplicates = true;

// Set result properties

kwq.QueryText = column + ":" + value;
// Fetch data
ResultTableCollection results = kwq.Execute();
ResultTable resultTable = results[ResultType.RelevantResults];

// Load data into a DataTable
DataTable tbl = new DataTable();
tbl.Load(resultTable, LoadOption.OverwriteChanges);

return tbl;

Hope this will help you building your own custom search webparts in MOSS 2007

Geert van der Cruijsen

Share on Facebook
Kick It on
Shout it
Post on Twitter