Thursday 15 November 2007

SharePoint 2007: Creating a custom advanced search webpart - Part 2

In the first part of this series of posts, we explained how to create a custom advanced search box by default which limits searches to a particular search scope.

In this part we will explain how to create a advanced search customization which uses a dropdown to limit results to a particular set of meta data / categories only.

Typical search requirements would be that users can limit their search results to a particular category of results only. An example of this would be the following: You have created a number a document library(ies) each having a DocType column which identifies whether the uploaded document is a Purchase Order, Invoice, Contract, Tender etc. You then want to create a search customisation such that your users can choose to search within Purchase Orders only.

This can be done using a number of sharepoint features and the advanced search custom webpart we will build.

Defining a searchable property

The first thing that we need to do is ensure that the DocType column we have created to categorize our documents is a searchable. To do this we need to go to the Shared Services > Search Settings > Metadata Property Mappings in Central Administration. As the description in this page says, "Users can perform queries over managed properties". Therefore we need to create our own mapped data property, and map this property to the DocType column we have created.

Click on the New Managed Property, give the property a name (DocType), and specify that this will be a text property. In the Mappings to crawled properties you need select the property to map to by clicking on Add Mapping and selecting the DocType column. This is done by searching for the DocType crawled property name. (If you don't find it here, you need to perform a full crawl, so that your column is discovered, and then re-do the steps here).

Once this is done you need to perform a full crawl again to make sure that the Number of items found with this property has a number which is greaten than zero. This means that items which use the DocType have been discovered.

Creating a custom advanced search dropdown which searches this property

Once again, we need to create a webpart page with a Content Editor Web Part and a Search Core Results web part. Using the source editor we define the following code:

The ASB_TextDT_Props defines the properties which will be used to define the text properties which will be used to build the query.

Quoting Tom Clarkson:

"ASB_TextDT_Props and ASB_DateTimeDT_Props Are lists separated by "#;#" which specify the properties which will be treated as text and datetimes respectively when building the query. All others are treated as numbers. "

Thus in our case:
<input type="hidden" name="ASB_TextDT_Props" id="Hidden4" value="DocType" />

We then define the search such that only items that contain certain values are displayed in the search using the following:

<input name="nameprefix$ASB_PS_plb_1" type="hidden" value="DocType" /> <input name="nameprefix$ASB_PS_olb_1" type="hidden" value="Contains" />

ASB_PS_plb_1 is the internal name of the property to search (in our case DocType)

whilst ASB_PS_olb_1 is the operator which will be used (contains)

The next thing is to actual build the drop down list:
<SELECT name="nameprefix$ASB_PS_pvtb_1"> <OPTION></OPTION>
<OPTION>Purchase Order</OPTION>
<OPTION>Invoice</OPTION>
<OPTION>Contract</OPTION>
<OPTION>Tender</OPTION>
</SELECT>

Once this is done, the only thing which is left is to create the text box to enter the queries to search for:
<input name="nameprefix$ASB_TQS_AndQ_tb" type="text" />

This used the ASB_TQS_AndQ_tb to define the All words in query operator.

The last thing which is required is the Submit button which posts to the CustomSearch.aspx page (which is the page which contains our Search Core Results Page):

<input type="hidden" name="nameprefix$ASB_SS_scb_1_4" value="nameprefix$ASB_SS_scb_1_4"/></table> <INPUT id="Submit1" onclick='WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("nameprefix$ASB_BS_SRCH_1", "", false, "", "/Pages/Pages/CustomSearch.aspx", false, false))' type="submit" name="nameprefix$ASB_BS_SRCH_1" value="Search" />

All Code:

<input type="hidden" name="ASB_TextDT_Props" id="Hidden4" value="DocType" />

<input name="nameprefix$ASB_PS_plb_1" type="hidden" value="DocType" /> <input name="nameprefix$ASB_PS_olb_1" type="hidden" value="Contains" />

<SELECT name="nameprefix$ASB_PS_pvtb_1"> <OPTION></OPTION>
<OPTION>Purchase Order</OPTION>
<OPTION>Invoice</OPTION>
<OPTION>Contract</OPTION>
<OPTION>Tender</OPTION>
</SELECT>

<input name="nameprefix$ASB_TQS_AndQ_tb" type="text" />

<input type="hidden" name="nameprefix$ASB_SS_scb_1_4" value="nameprefix$ASB_SS_scb_1_4"/></table> <INPUT id="Submit1" onclick='WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("nameprefix$ASB_BS_SRCH_1", "", false, "", "/Pages/Pages/CustomSearch.aspx", false, false))' type="submit" name="nameprefix$ASB_BS_SRCH_1" value="Search" />

This creates a dropdown custom advanced search which limits the search results to the particular category you have chosen. For more details on how this was done and what each operator does, what they stand for and more operations which you can use in your custom queries visit Tom Clarkson's post.