Search WebPart for Sharepoint 2010

By tdomf_6998b, on October 19th, 2010
This is a free webpart. Below is the complete code. Enjoy!

In SharePoint 2010, you can use the query object model to build custom
Web Parts and search applications. This object model allows you to
query against SharePoint Server search as well as to FAST Search Server 2010 for

The FullTextSqlQuery class available in the object model allows you to build
complex search queries based on SQL syntax however, the KeywordQuery class can
be used to issue search queries based on simple query syntax. With Fast Search
Server 2010, this class can also be used to run queries based on the Fast Query
Language (FQL), which supports advanced queries in enterprise environments.

The example below demonstrates using the query object model to issue a search
query based on a keyword search with the KeywordQuery class. This example is
built as an application page with a text input field called txtSearch, a button
for submitting the query called btnSearch, and an SPGridView called

Please Note : Before you can run search queries, you also need to specify the
search server that should be used for indexing in Central Administration, under
Application Management -> Databases -> Manage Content Databases ->
[Your content DB] > Search Server.

using System;
using Microsoft.SharePoint;
using System.Data;
using Microsoft.Office.Server.Search;

namespace Apress.SP2010.Layouts.CustomSearch
public partial class
ApplicationPage1 : LayoutsPageBase
protected void Page_Load(object
sender, EventArgs e)

// The event is called when the search button is pressed
protected void
btnSearch_Click(object sender, EventArgs e)
DataTable search = new

using (SPSite site = new
KeywordQuery query = new

query.ResultsProvider = Microsoft.Office.Server.Search.Query.


query.ResultTypes = ResultType.RelevantResults;

query.KeywordInclusion = KeywordInclusion.AllKeywords;

query.QueryText = txtSearch.Text;

ResultTableCollection results = query.Execute();

if (results.Count > 0)

ResultTable relevant = results[ResultType.RelevantResults];


DataView view = new DataView(search);

// Fill the SPGridView defined in the page markup

searchGrid.DataSource = search;


private void SetGridColumns()
SPBoundField fieldTitle = new

fieldTitle.HeaderText = “Title”;

fieldTitle.DataField = “Title”;


SPBoundField fieldPath = new SPBoundField();

fieldPath.HeaderText = “Path”;

fieldPath.DataField = “Path”;


SPBoundField fieldAuthor = new SPBoundField();

fieldAuthor.HeaderText = “Edited”;

fieldAuthor.DataField = “Write”;


In the above code, the OnClick handler for the button creates a KeywordQuery
object and passes the text from the text field to the QueryText property. After
calling Execute, the results are available through the ResultTable at the index
ResultType.RelevantResults in the returned ResultTableCollection. Using a
DataTable, these results are bound to the SearchGrid for display. The relevant
table columns are bound using the SetGridColumns method during Page_Load.

Saiba mais sobre os recursos do Sharepoint


Installing FullText Search on SQL Server 2008

1. DownloadSQL Express with FullText Search capability for SQL Server 2008

2. Select Installation Type as New Installation

3 During Feature Selection, select Full-Text Search

4. While Instance Configuration, Create new Instance Configuration.
– Select Named Instance and give new name.  Example: I have given name: MyShriGanesh

5. Main steps for installation are done, now simply press next with default choices and complete the installation.

6. After Installation login with new instance name created.
– Example: Earlier I have created named instance MyShriGanesh, so i need to login with same named instance in order to take benefit of FullText Search feature.

7. Now, Create New Database, Run Tables and SPs Scripts and restore database with data.  If you don’t have one already, download sample database and use that database.

After installation of sample database your object explorer will look like as follow:

8. Open Query Window and type following command to test whether FullText Search is installed or not.

Select SERVERPROPERTY(‘IsFullTextInstalled’)
If this query returns a value of ‘1’ then the component is installed.

Now, lets go step by step to run Full Text Search on SQL Server 2008

Step 1: Creating Full Text Catalog in SQL Server 2008
Create FullText Catalog DatabaseNameCatalog

Create FullText Catalog AdventureWorksCatalog

Step 2: Create FullText Index in SQL Server 2008
Create FullText Index on TableName
(Column1, Column2, …., ColumnN)
Key Index PK_TablesPrimaryKey on DatabaseNameCatalog
with Change_Tracking Auto
  • TableName is name of table you would like to create fulltext index.
  • ColumnName is column on which you would like to create fulltext index, it is column you would like to perform search on.
  • PK_TablesPrimaryKey is primary key of table you are creating on fulltext search index.
  • DatabaseNameCatalog is fulltext search catalog created earlier.
Create FullText Index on Production.ProductDescription
Key Index PK_ProductDescription_ProductDescriptionID on AdventureWorksCatalog
with Change_Tracking Auto


Step 3: Running FullText Search Query
There are many different examples of running fulltext query, I would be giving 2 examples of fulltext search query.
Example 1: Using FreeText

Select [Description]
from Production.ProductDescription
FREETEXT([Description],  ‘Entry Level Bike’)

Example 2: Using Contains
Select [Description] 
from Production.ProductDescription
Contains([Description],  ‘”Entry” and “Level” and “Bike”‘)

Example 3: Using Weight keyword
Select [Description] 
from Production.ProductDescription
‘ISABOUT (Entry Weight(.8), Level Weight(.4), Bike Weight (.2))’)



Example 4: Using Inflectional keyword

Select [Description]
from Production.ProductDescription
‘FormsOf (INFLECTIONAL, Entry, Level, Bike)’)