Customize SharePoint Search Rendering

21 Jul

This morning I threw together some quick instructions for a client on customizing the Core Results Web Part and adding additional fields to the MOSS Search Results.  I hadn’t planned on posting it, but as I already had it done I thought it might help someone else.  Certain text blacked out to protect the innocent!

 

Here we will be customizing the People Results page by adding a new link to the results, which passes the account name of the user as a query string parameter.  This process would be the same to add any field to any of the results pages.

 

All the results rendering for your main search results is in the core results web part, so here I do a People search, edit the page and modify the core results part:

image

The part passes a bunch of data to the XSL we provide for rendering, but by default the field I would like to use, AccountName isn’t passed.  You can pass anything that a managed property exists for in the Managed Properties section of the Shared Service provider (under Search Settings).

 

If you look at the metadata property mappings, there is an Account Name already defined and mapped to the AD import field “People:AccountName(text)”.  This is the field I’ll add to my search results.

Note: If you add a new field here, You’ll need to do a full search crawl before it shows up.

image

So back in our Search Core Results edited web part, we choose “Selected Columns” under query options, and add this column into the mix:

clip_image005

You’ll want to paste these snippets into an XML or text editor when you modify them, as the window that pops up is useless.  You’ll add one more entry to the list as shown below, for AccountName (case sensitive here)

clip_image007

Paste that back into the web part property, and go edit the XSL property:

clip_image009

The XSL in there is relatively small and manageable.  You’ll find a template called “Results” which is the actual result line-item rendering.  It has several div’s it renders out, Title, Description, srch-Metadata, etc. 

 

The Search Metadata div is where they fit the URL’s and such at the bottom of each entry, so it is where we want to add our link today.  You could rewrite the entire results rendering if you’d like. 

I did a simple case that just tacks the URL built from our new column on the next line.  See the two additions in red I made to the XSL.

Note: Confusingly, the XSL is case sensitive on these columns as well, except it’s a different case, they have to be all *lower* case.  Example:  In the columns XML I put “AccountName” but when I use it in the XSL, I must use “accountname”.

 

My changes are below in red outline, they are as follows:

  1. Added new variable called account based on our accountname column that was passed in.  I will use this variable in string concatenation to generate my URL.
  2. Used that variable (As well as the column) down below to add a new link in an HTML <a> tag.

clip_image011

 

When you are done editing, paste your XSL back into the XSL property for the web part (hopefully yours is prettier than mine) and you’ll get your new results:

 

image

If it tells you it can’t render the web part after you hit apply, something is wrong with your XSL.

 

Note: For more advanced XSL editing, you may want to use a real XSL editor such as Oxygen XML, which will let you test, debug, and preview your results.  To do this you’ll need to get a sample of the XML to work with, which you can do by pasting an “Identity” XSL into the XSL property, viewing the page source, and capturing your XML.

Tweet about this on TwitterShare on Google+Share on FacebookShare on RedditEmail this to someoneShare on LinkedInShare on StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *