Tuesday, May 1, 2012

SharePoint Search through Server Object Model 2010

In this article we will discuss how we can do SharePoint 2010 enterprise search through SharePoint 2010 object model. Also you can check my last article on FAST Search Server 2010 SharePoint 2010 .

SharePoint Search is very much important for enterprises. We will see how we can do free text search trrough SharePoint 2010 object model. There are two very much important classes for SharePoint 2010 object model to use:

1. FullTextSqlQuery: This issues full-text SQL syntax queries to the search provider.

2. KeywordQuery: This issues keyword syntax queries.

Below is a sample code for search:
using (SPSite thisSite = new SPSite("http://Site URL"))
{
string keyWord = "Sample Keyword";


KeywordQuery query = new KeywordQuery(thisSite);

query.RowLimit = 100;

query.QueryText = keyWord; // This is a free text and it can be a single word or a phrase. But the character limit is 2048 characters.

query.ResultTypes = ResultType.RelevantResults; // Here ResultsTableCollection is an IEnumerable collection of ResultTable objects.

ResultTableCollection results = query.Execute();
// If you want to see only relevant results then

ResultTable relevantResults = results[ResultType.RelevantResults];

// You can also load the data into data table

DataTable dt = new DataTable();
dt.TableName = "Search Results";
dt.Load(relevantResults, LoadOption.OverwriteChanges);

}
}
You can also see some Search Articles here.

No comments:

Post a Comment