satish babu

satish babu

  • 1.1k
  • 633
  • 85.3k

SharePoint Custom search from C#.net application

Jun 25 2014 6:59 AM
Hi,
 
I have a small problem with coding is, I implemented custom search for my application purpose. So it require to show the search content from that particular site only but code is showing the relative results from all the sites. Can any one help to get the searchable content from only that particular site only.
 
Here is the code
 

using System;

using System.ComponentModel;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.Office.Server;

using Microsoft.Office.Server.Search;

using Microsoft.Office.Server.Search.Query;

using Microsoft.Office.Server.Search.Administration;

using System.Data;

namespace SharePointCustomSearchWP.searchWP

{

[ToolboxItemAttribute(false)]

public class searchWP : WebPart

{

protected Button btnSearch;

protected TextBox txtSearchBox;

protected Label lblResults;

protected GridView gridSearchResults;

protected override void CreateChildControls()

{

txtSearchBox = new TextBox();

this.Controls.Add(txtSearchBox);

lblResults = new Label();

this.Controls.Add(lblResults);

btnSearch = new Button();

btnSearch.Text = "Find";

btnSearch.Click += new EventHandler(btnSearch_Click);

this.Controls.Add(btnSearch);

}

void btnSearch_Click(object sender, EventArgs e)

{

if (string.IsNullOrEmpty(txtSearchBox.Text))

{

lblResults.Text = "Please enter words to search for";

return;

}

ExecuteKeywordQuery(txtSearchBox.Text);

}

void ExecuteKeywordQuery(string keywordQueryText)

{

//Get a proxy for Search Service Application(SSA). The SSA serves search queries.

SearchServiceApplicationProxy SSAProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));

KeywordQuery keywordQuery = new KeywordQuery(SSAProxy);

//keyword search using default provider. We can use "FAST" or "SharepointSearch" to be specific.

keywordQuery.ResultsProvider = SearchProvider.Default;

keywordQuery.QueryText = keywordQueryText;

keywordQuery.ResultTypes |= ResultType.RelevantResults;

ResultTableCollection searchResults = keywordQuery.Execute();

if (searchResults.Exists(ResultType.RelevantResults))

{

ResultTable searchResult = searchResults[ResultType.RelevantResults];

DataTable result = new DataTable();

result.TableName = "SearchResults";

result.Load(searchResult, LoadOption.OverwriteChanges);

PopulateResultsGrid(result);

}

}

private void PopulateResultsGrid(DataTable resultsTable)

{

gridSearchResults = new GridView();

gridSearchResults.DataSource = resultsTable;

gridSearchResults.DataBind();

Controls.Add(gridSearchResults);

}

protected override void Render(HtmlTextWriter writer)

{

writer.Write("<table width='100%'><tr><td>");

txtSearchBox.RenderControl(writer);

writer.Write("</td><td>");

btnSearch.RenderControl(writer);

writer.Write("</td></tr><tr><td colspan='2'>");

lblResults.RenderControl(writer);

writer.Write("</td></tr><tr><td colspan='2'>");

if (gridSearchResults != null)

gridSearchResults.RenderControl(writer);

writer.Write("</td></tr></table>");

}

}

}


Answers (1)