using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Search;
using Microsoft.SharePoint.Search.Query;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Search;
using Microsoft.SharePoint.Search.Query;
public void keywordQueryExecute(string strQueryText) //Pass the search text here
{
using (SPSite _SPSite = new SPSite("http://IP ADDRESS HERE/sites/SITE NAME HERE/"))
{
using (FullTextSqlQuery _FullTextSqlQuery = new FullTextSqlQuery(_SPSite))
{
_FullTextSqlQuery.StartRow = 0;
_FullTextSqlQuery.HighlightedSentenceCount = 3;
_FullTextSqlQuery.KeywordInclusion = KeywordInclusion.AnyKeyword;
_FullTextSqlQuery.ResultTypes = ResultType.RelevantResults;
_FullTextSqlQuery.RowLimit = 5000;
_FullTextSqlQuery.TrimDuplicates = true;
_FullTextSqlQuery.EnableStemming = false;
_FullTextSqlQuery.IgnoreAllNoiseQuery = true;
_FullTextSqlQuery.SiteContext = new Uri(_SPSite.Url);
if (SPSecurity.AuthenticationMode != System.Web.Configuration.AuthenticationMode.Windows)
_FullTextSqlQuery.AuthenticationType = QueryAuthenticationType.PluggableAuthenticatedQuery;
else
_FullTextSqlQuery.AuthenticationType = QueryAuthenticationType.NtAuthenticatedQuery;
StringBuilder sbFullTextSqlQuery = new StringBuilder(string.Empty);
sbFullTextSqlQuery.Append("SELECT ");
sbFullTextSqlQuery.Append(" FileExtension,Title,Description,HitHighlightedSummary,Path,Author,Size,LastModifiedTime ");
sbFullTextSqlQuery.Append("FROM ");
sbFullTextSqlQuery.Append(" SCOPE() ");
sbFullTextSqlQuery.Append("WHERE ");
sbFullTextSqlQuery.Append(" Site= ");
sbFullTextSqlQuery.Append(" '" + _SPSite.Url.ToString() + "' ");
sbFullTextSqlQuery.Append(" AND ");
sbFullTextSqlQuery.Append(" ( FREETEXT (DefaultProperties, '" + strQueryText + "') ");
sbFullTextSqlQuery.Append(" OR ");
sbFullTextSqlQuery.Append(" CONTAINS (*, '\"" + strQueryText + "*" + "\"')) ");
sbFullTextSqlQuery.Append(" ORDER BY ");
string strSort = " Rank Desc";
sbFullTextSqlQuery.Append(strSort);
//sbFullTextSqlQuery.Append(" Rank Desc");
_FullTextSqlQuery.QueryText = sbFullTextSqlQuery.ToString();
try
{
ResultTableCollection resultTables = _FullTextSqlQuery.Execute();
if ((int)ResultType.RelevantResults != 0)
{
ResultTable tblResult = resultTables[ResultType.RelevantResults];
if (tblResult.TotalRows == 0)
{
lblQueryResult.Text = "No Search Results Returned.";
}
else
{
ReadResultTable(tblResult);
lblQueryResult.Text = "";
}
}
}
catch (Exception Ex)
{
lblQueryResult.Text = Ex.Message;
}
}
}
}
public void ReadResultTable(ResultTable rt)
{
// Create a DataSet and load the returned ResultTable into it.
DataTable relResultsTbl = new DataTable();
relResultsTbl.TableName = "Relevant Results";
DataSet ds = new DataSet("resultsset");
ds.Tables.Add(relResultsTbl);
ds.Load(rt, LoadOption.OverwriteChanges, relResultsTbl);
// Add the results to the DataGrid.
fillResults(ds);
}
Finally the DataSet ds will have the result. You can bind to gridview or table.