Normally we use out of the Box SharePoint Search, but sometimes there is custom requirement in which we need to use Custom Search Webpart.
Here is a small portion of code to write for custom Enterprise Search Webpart.
Sample Code:
protected System.Web.UI.WebControls.DataGrid grdEntrerpriseSearchResults = new DataGrid();
protected override void CreateChildControls()
{
string strSearchQry;
using (SPSite site = new SPSite(SPContext.Current.Site.Url .ToString()))
{
strSearchQry = BuildFullTextSqlQuery(false);
Microsoft.Office.Server.Search.Query.FullTextSqlQuery qryFullText = newMicrosoft.Office.Server.Search.Query.FullTextSqlQuery(site);
qryFullText.QueryText = strSearchQry;
qryFullText.ResultTypes = Microsoft.Office.Server.Search.Query.ResultType.RelevantResults;
qryFullText.TrimDuplicates = true;
qryFullText.EnableStemming = true;
qryFullText.RowLimit = 50;
qryFullText.Timeout = 120000;
Microsoft.Office.Server.Search.Query.ResultTableCollection results = qryFullText.Execute();
Microsoft.Office.Server.Search.Query.ResultTable relevantResults = results[Microsoft.Office.Server.Search.Query.ResultType.RelevantResults];
DataTable resultsDataTable = new DataTable();
resultsDataTable.Load(relevantResults, LoadOption.OverwriteChanges);
grdEntrerpriseSearchResults.DataSource = resultsDataTable;
grdEntrerpriseSearchResults.DataBind();
}
this.Controls.Add(grdEntrerpriseSearchResults);
}
private string BuildFullTextSqlQuery(bool bDocumentsOnly)
{
string sRetval = string.Empty;
string sSelectQuery = "SELECT Title, Path, Size, HitHighlightedSummary FROM Scope() ";
string sWhereQuery = string.Empty;
sWhereQuery = " WHERE (\"SCOPE\" = 'SiteName') AND (IsDocument=1) ";
if (bDocumentsOnly == true)
sWhereQuery = " WHERE IsDocument=1 AND (\"SCOPE\" = 'All')";
else
sWhereQuery = " WHERE IsDocument=0 AND (\"SCOPE\" = 'All')";
sWhereQuery += " AND FREETEXT(*, 'Keyword') ";
string sOrderByQuery = " ORDER BY Rank DESC ";
if (bDocumentsOnly == false)
{
sWhereQuery += " AND ContentClass='STS_Web'";
}
sRetval = sSelectQuery + sWhereQuery + sOrderByQuery;
sRetval = sSelectQuery + sWhereQuery + sOrderByQuery;
return sRetval;
}
No comments:
Post a Comment