Friday, March 18, 2011

How to Add Custom Columns to the Search Results in MOSS 2007

A few years ago I demonstrated at the New Zealand Conference how to add custom metadata columns to your search results. What I never did was make a blog post about it. So let me rectify that error of mine.

To get custom columns to appear on your search results page in MOSS you will need to do a few things. First make sure the metadata field is indexed and available by adding it to the Mapped Metadata Columns in the Search Administration of your SSP. Then, recrawl your index. And finally show the new column on the search results page by tweaking some xml and xsl.

1) Get the column indexed and mapped
To do this, open up your SSP admin site either directly or via Central Admin. Then navigate to the Search Administration Page and look for Managed Columns on the left menu. There you will need to add a new managed colum which mapps your custom field. This field could be in a SQL Store indexed via BCS or in a custom list or in a custom content type. In any case I assume that you have already crawled all the content in question, otherwise this step won't work.
2) Recrawl the index
Once you have added the managed metadata colum you need to do a full crawl. Sorry, no incremental crawls will work, as only in a full crawl does the engine do the mapping of all existing fields in the index.
3) Customise the Search Results
Browse to the search results page in your Search Centre. Fastest way? Type in a search into any search box! If you farm is configured correctly that should catapult you to the correct page.
There you need to edit the page and customise the Core Search Results WebPart.

Before you edit the actual XSL Look and feel you need to tell the webpart about the new column. So first click the three dots next to "Selected Columns". You find this option under the ResultsQuery Option. (refer to above screenshot) Copy the text out of the editor into notepad. You'll get this:
Well, you won't get that exactly. It will all be on one line and illegible. Feel free to use an xml editor of your choice instead of notepad.
In that xml you now need to add the Mapped Metadata column name. Make sure you use the correct case. XML is case sensitive! Also add the name you gave the mapped column and not the source metadata column (in case these two have different names). Once you have added the column to the xml text copy it back into the popup for the "Select Columns: option.

(note the new MyCustomColumn entry at the bottom)
Once you have added the xml for the new column you can add the column to the output in the XSLT.
Click the XSL Editor button on the WebPart customisation interface (see first screenshot) and add the new field to your xsl.

Extract before you add the markup:
And now with the new data appearing right next to the description of the search result:

That's it. Copy the changed xsl back into the XSL editor popup, apply your changes to the web part and test your results by executing a fresh search which includes some of the custom metadata that you have indexed.
The next step would be to create a custom scope for your BCD indexed data, create a custom search page and search results page for that scope and go ape-shit on the search results page with loads of custom columns and custom look and feel.
Happy Customising folks!

No comments: