Wednesday, June 25, 2008

Custom Searching on Meta data Programmatically

Custom Searching on Meta data Programmatically:

One of the greatest features of MOSS 2007 is to have user defined Meta data properties, you can have lots and lots meta data attributes associated with your list item, document library documents.

Recently I was working on document management system, where user had tons of properties to be associated with documents in there document library. As you know one of the best ability of MOSS 2007 is searching, unfortunately not everything in MOSS 2007 comes out of box, just like your custom Meta data properties will not come by default on Advance Search Box, but to enable it requires simple step. I am assuming you know how to configure advance search box web part. Nevertheless it is very easy, just drop Advance search box web part and Search core result web part on your page (Can be on separate page as well). In advance search web part, specify the path for the result page that’s it your good to go for searches. Crawl the web site and you will see searches coming up.
Now to configure search on your custom Meta data properties follow the following steps:
1. All your custom properties are not by default crawled, to make it crawled by search engine your need to map it first.
2. To map goto Central Admin >> Shared Service Provider >> Search Settings >> Map Metadata Properties
3. Click New Managed Property Name it and add mapping to it , For example you will give your property name “PrpDocumentCategory” , once you click add mapping type in the name you gave your column and click find MOSS will fetch it for you select it and hit ok and OK again.
4. Repeat this step For all your Custom properties.
5. Crawl your Web Application

Now you have successfully mapped your custom properties to crawled properties , but still you won’t see list of your properties in Advance Search Box To enable it another simple configuration is required:

1. Take properties of your advance search box web part and click properties and select browse button , a dialog will appear copy the entire XML to notepad for easy editing and save it as xml and open in it on some XML editor.
2. Find the section and add following tag for all of your mapped properties :

In our case

Repeat for all of your properties.
3. In Last section , that section you want property to appear like all result , documents ,excel file etc and insert following tags


Now you will see every property in drop down list corresponding to the result type you selected. Search for it you will see every thing working perfect.
Now lets go into programming side , now what if you want search to consume in your custom web page or web part or as a matter of fact custom windows application.
MOSS 2007 give 2 option to do search programmatically :
• Creating Search Queries Programmatically by Using the Search Object Model in SharePoint Server 2007
• Creating Search Queries Programmatically by Using the Search Web Service in SharePoint Server 2007

Lets go into sample script using object model of sharepoint assuming your in sharepoint context (web part or web page):

1. First step you need to reference following Assembly : “Microsoft.Office.Server.Search.dll”
2. Include following name space in your code:
a. using System;
b. using System.Runtime.InteropServices;
c. using System.Web.UI;
d. using System.Web.UI.WebControls.WebParts;
e. using System.Xml.Serialization;
f. using Microsoft.Office.Server.Search;
g. using Microsoft.Office.Server.Search.Query;
h. using Microsoft.SharePoint;
i. using Microsoft.SharePoint.WebControls;
j. using Microsoft.SharePoint.WebPartPages;
k. using System.Data;

protected override void Search()
{

String strEmployeeID=”ABC”;
SPSite site = SPContext.Current.Site;

// Create new site from current context

KeywordQuery kwq = new KeywordQuery(site);

// create keyword object


kwq.QueryText = "EmployeeID:"+strEmployeeID;

// set the query text Where EmployeeID property is equal to ABC
// to include property you can use plus sign and to exclude property value from result you can use minus sign
// for example EmployeeID:ABC -DepartmentID:HR
// this will fetch all documents having property value EmployeeID abc but not those document having DepartmetnID:HR

kwq.ResultTypes =ResultType.RelevantResults;

ResultTableCollection results =kwq.Execute();

//execute the Query

ResultTable resultTable =results[ResultType.RelevantResults];

//get the first collection of relevlant result

while (resultTable.Read())
{
resultTable.GetString(5);
resultTable.GetString(2);
resultTable.GetString(6);
resultTable.GetString(8);
resultTable.GetString(3);
resultTable.GetString(4);
}
// iterate the result set do formatting or what ever u want, consume result in web part or page
}

Next time I will tell you how to consume MOSS 2007 search web service and how to use full text SQL query in MOSS 2007 OBJECT model.For more quires you can shoot me an email.



Kind Regards :

Muhammad Kaisar Wadiwala