As you know, SharePoint allow to define Search scope and condition to create full text search indexes. This is really one of greatest features SharePoint bring to you.

All search scopes will be managed by Share Services Provider (SSP). It’s easy to create a custom search scopes with friendly user interface.

You can see more details here:

For SharePoint 2007: http://dattard.blogspot.com/2007/04/sharepoint-2007-creating-custom-search.html

For SharePoint 2010: http://technet.microsoft.com/en-us/library/ff453895.aspx

In this post, I would like to focus in Developer side, SharePoint not only provide a powerful feature with ability to index many content, break down the content into different scopes, dynamic and friendly search for end user, but also Developer can query in each existed scopes like a Table  from SQL. It’s awesome!!!

See image below, I created some search scope: All Sites, Poeple, Document Libraries

From now, we can query on each scope as well as example code below

– Query to People Search Scope

DataTable resultDataTable = new DataTable();string SQLQueryString = “SELECT PersonalTitle, Firstname, Lastname, AccountName, Department, workemail”+
“FROM SCOPE() “+
“WHERE \”scope\”=’People’ ORDER BY Write DESC”;using (FullTextSqlQuery searchQuery = new FullTextSqlQuery(ServerContext.Current))
{
SPSite contextSite = SPControl.GetContextSite(HttpContext.Current);

searchQuery.SiteContext = new Uri(contextSite.Url);
searchQuery.StartRow = 1;

if (rowLimit > 0)
{
searchQuery.RowLimit = 500;
}

searchQuery.EnableStemming = true;
searchQuery.KeywordInclusion = KeywordInclusion.AllKeywords;
searchQuery.ResultTypes = ResultType.RelevantResults;
searchQuery.TrimDuplicates = true;
searchQuery.QueryText = SQLQueryString;

ResultTableCollection searchResultTableCollection = searchQuery.Execute();
if ((int)ResultType.RelevantResults != 0)
{
searchResultTable = searchResultTableCollection[ResultType.RelevantResults];
}
}

– Or more simple to query to All Sites Search Scope

var query = new FullTextSqlQuery(SPContext.Current.Site)
{
QueryText = “SELECT Title FROM SCOPE() WHERE \”scope\” = ‘All Sites’ AND  CONTAINS(Title, ‘Example’)”,
ResultTypes = ResultType.RelevantResults
};
var queryResults = query.Execute();
var queryResultsTable = queryResults[ResultType.RelevantResults];
var results = new DataTable();
results.Load(queryResultsTable, LoadOption.OverwriteChanges);

That’s all, You can manually control search result and freely customize SharePoint search with available powerful full text search. So cool!

Related article:

Hoang Nhut Nguyen
Email: nhutcmos@gmail.com
Skype: hoangnhut.nguyen