SharePoint search using Object Model, FullTextSqlQuery

Today, I will be posting a small snippet demonstrating SharePoint search using FullTextSqlQuery.
First Add References to required NameSpace
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Query;

And the code goes here

//Here SharedServices2 refers to the Shared Service Provider servicing the site in Question
ServerContext context = ServerContext.GetContext("SharedServices2");
FullTextSqlQuery qrySearch = new FullTextSqlQuery(context);
//Specifies the results set containing the main search results from the content index matching the search query.
qrySearch.ResultTypes = ResultType.RelevantResults;
//Sremming : expansion of searches to include plural forms and other word variations :-)
qrySearch.EnableStemming = true;
qrySearch.TrimDuplicates = false;
qrySearch.QueryText = "SELECT URL, Title, Size, Write, HitHighlightedSummary FROM SCOPE() where \"scope\"='NTFS Archive' And FREETEXT(*,'" + TextBox1.Text.Trim() + "')";
ResultTableCollection results = qrySearch.Execute();
ResultTable resultTable = results[ResultType.RelevantResults];
// Data Table to Store Search Results
DataTable dtResults = new DataTable();
dtResults.Load(resultTable, LoadOption.OverwriteChanges);

No comments:

Post a Comment