Tuesday 28 May 2013

Creating custom search web part

Creating custom search web part

This demo shows how to build a custom search web part for SharePoint 2007. This custom search web part will search for all types of documents (For ex. doc, docx, xls, xlsx, pdf etc).
This is simple web part, which have a text box for enter the query text, a drop down for selecting the document type, a button and a label for displaying the error message.

By default SharePoint doesn't support Pdf documents. For working with pdf documents in SharePoint, we need to install PDF iFilter (We can download Pdf iFilter v6.0 from http://www.adobe.com/support/downloads/thankyou.jsp?ftpID=2611&fileID=2457)

We can create a custom web part in different ways (Soon I will explain the different ways). In this demo, I am creating the web part by using Visual Studio 2005 class library project.

Steps to create a web part:
1. Create a new project of type Class library in Visual Studio 2005.
2. Add the following references

  • Microsoft.Office.Server
  • Microsoft.Office.Server.Search
  • Microsoft.SharePoint
3. Inherit the class from 'Microsoft.SharePoint.WebPartPages.WebPart' class.
namespace CustomSearchWebPart
{
  public class DocumentSearch : Microsoft.SharePoint.WebPartPages.WebPart
  {
3. Now by Create CreateChildControls() method, we can define the UI for web part.
In this method we need to instantiate the web controls and design the UI for our web part.

//Variable decleration
TextBox txtQueryText; //System.Web.UI.WebControls.TextBox
Button btnSearch;
Label lblResult = new Label();
DropDownList ddlDocumentType;
protected override void CreateChildControls()
{
this.Controls.Add(new LiteralControl("<html><body>"));
this.Controls.Add(new LiteralControl("<table width='100%' ><tr><td style='width: 100%; height: 10px;'> "));
btnSearch = new Button();
btnSearch.Text = "Submit";
btnSearch.Font.Name = "Verdana";
btnSearch.Font.Size = new FontUnit(8, UnitType.Point);
btnSearch.Height = new Unit(20, UnitType.Pixel);
btnSearch.Click += new EventHandler(btnSearch_Click);
this.Controls.Add(btnSearch);

this.Controls.Add(new LiteralControl("</td></tr>"));


By this way, we can define the UI for our custom web part.

And I will define following method, which will execute when user clicks on this button control.


void btnSearch_Click(Object sender, EventArgs e)
{
//Business logic will come here
FullTextSqlQuery fRequest = new FullTextSqlQuery(new SPSite(""));
fRequest.QueryText = "SELECT author,title FROM scope() where contains(' " + keyvalue + " ') AND \"scope\" ='Word Docs' ";
fRequest.ResultTypes = ResultType.RelevantResults;
ResultTableCollection resultTbls = fRequest.Execute();
if ((int)ResultType.RelevantResults != 0)
{
ResultTable tblResult = resultTbls[ResultType.RelevantResults];
if (tblResult.TotalRows != 0)
{
DataTable relResultsTbl = new DataTable();
relResultsTbl.TableName = "Relevant Results";
DataSet ds = new DataSet("resultsset");
ds.Tables.Add(relResultsTbl);
ds.Load(tblResult , LoadOption.OverwriteChanges, relResultsTbl);
}
}
}

Now the dataset 'ds' contains the results and on Grid View control, we can display the results.

No comments:

Post a Comment