//Namespace to use for search
using System.Data;
using Microsoft.Office.Server.Search.Query;
using Microsoft.Office.Server.Search.Administration;
// inside create child of your web part pub this code
protected override void CreateChildControls()
{
Controls.Clear();
queryTextBox = new TextBox();
this.Controls.Add(queryTextBox);
queryButton = new Button();
queryButton.Text = "Start Search";
queryButton.Click += new EventHandler(queryButton_Click);
this.Controls.Add(queryButton);
resultsLabel = new Label();
this.Controls.Add(resultsLabel);
}
//button click method which will call the function to execute
void queryButton_Click(object sender, EventArgs e)
{
if (queryTextBox.Text != string.Empty)
{
ExecuteKeywordQuery(queryTextBox.Text);
}
else
{
resultsLabel.Text = "You must enter a search word.";
}
}
//method to execute the query and fetch the results
void ExecuteKeywordQuery(string queryText)
{
SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy
(SPServiceContext.GetContext(SPContext.Current.Site));
KeywordQuery query = new KeywordQuery(proxy);
query.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
query.QueryText = queryText;
query.ResultTypes|=ResultType.RelevantResults;
ResultTableCollection searchResults = query.Execute();
if(searchResults.Exists(ResultType.RelevantResults))
{
ResultTable searchResult = searchResults[ResultType.RelevantResults];
DataTable result = new DataTable();
result.TableName = "Result";
result.Load(searchResult,LoadOption.OverwriteChanges);
FillResultsGrid(result);
}
}
//generate and display the results in grid view
private void FillResultsGrid(DataTable resultTable)
{
//Instantiate the DataGrid
resultsGrid = new DataGrid();
//Set the DataSource
resultsGrid.DataSource = resultTable;
//Bind the data to the DataGrid
resultsGrid.DataBind();
//Add the DataGrid to the controls
Controls.Add(resultsGrid);
}
No comments:
Post a Comment
Thank you for Commenting Will reply soon ......