Implementing Actionable Search Results with Microsoft Office SharePoint Server 2007

17. June 2009

This article will show you how to customize the search results in a MOSS 2007 search center to add some useful action links to document results. This is a solution that I have had in the drawer for some time and recently I dusted it off for use in Webinars on Turning SharePoint Search into a better Navigation tool. The interest for getting the actual code has been overwhelming and I decided I better blog it now.

Out-of-the-box, SharePoint basically offers only one action on results; a link for opening a document in read-only mode. This is fine for Internet/Extranet scenarios where there are few documents and where users are not supposed to edit them. But for an Intranet collaboration portal the picture is different. Here users may work on many documents on multiple sites and in multiples document libraries. Not knowing or forgetting exactly where a document is stored or just not wanting to tediously browse their way down to a specific document is a fair and common story. This is where SharePoint search can help – just type one or more relevant keywords, click the search button and open the document without worrying too much on which site and in which document library it is stored. A typical document result in a default search center looks like this:

ActionableSearchResults1

But what if users want to perform additional actions on the document like check-in, check-out, view properties, etc? Ouch! No way to navigate to the document library where the document lives! Fortunately, SharePoint is a very flexible platform and the SharePoint search results can with fairly little effort be customized to include a link for navigating to the document library. While we are at it, why not also add a few more useful actions? The code sample that I am about to share below adds four actions to document results as illustrated below:

ActionableSearchResults2

Then Send link action opens a new email with a link to the document, Add to My Links opens a dialog where users can add the result to their My Links list, View properties redirects to the properties page of the document library list item from where the containing document library folder can also be accessed. Finally Edit document opens the document for editing and asks for check-out if required. The fifth View duplicates action is available OOB as illustrated in the first screen shot.

The sample implementation that you can download below is composed of two parts; a custom XSLT template for the Core Results Web Part plus an ASHX handler for the View Properties action. Let us first have a look at how the custom XSLT template implements each custom action link. The following table shows the HTML and Javascript code used in the template. Please consult the downloadable zip archive below or the link above for the full XSLT listing.

Action URL
Send link
<a href=”javascript:SendEmailWithLink(‘[url]’)”>Send link</a>

where [url] is the full path to the result item. The Javascript method SendEmailWithLink is implemented as follows:

function SendEmailWithLink(link) 
{ 
  var link = "mailto:?body=" + escapeProperly(link); 
  navigateMailToLink(link); 
  return false; 
}
Add to My Links
<a href=”javascript:AddToMyLink(‘[title]’,‘[url]’)”>Add to My Links</a>

where [title] is the item title and [url] is the full path to the item. The Javascript method AddToMyLink is implemented as follows:

function AddToMyLink(title, url) 
{
  var args = new Array();
  args[0] = title;
  args[1] = url;
  args[2] = '';
  var features = 'resizable=no,status=no,scrollbars=yes,'+
                 'menubar=no,directories=no,'+
                 'location=no,'+
                 'width=750,height=475';
  if (browseris.ie55up)
    features = 'resizable:no;status:no;scroll:yes;'+
               'help: no;center: yes;'+
               'dialogWidth:750px;dialogHeight:475px;';
  commonShowModalDialog(
    '/_layouts/QuickLinksDialog.aspx',features,null,args);
}
View properties
<a href=”/_layouts/actionredirect.ashx?url=[url]”>View properties</a>
Edit document
<a href="" onclick=”
  return editDocumentWithProgID2('[url]', '', 
                                 'SharePoint.OpenDocuments', 
                                 true, '', false)”>
Edit document
</a>

where [url] is the full path to the result item. The Javascript method editDocumentWithProgID2 is supplied by SharePoint.

The View Properties action can unfortunately not be implemented as easily as the other actions. It requires an extra layer of indirection as the search content index cannot give us the list item ID of documents in a document library. But we need this to redirect to the View Properties page having an URL of the form:

http://server/Docs/Documents/Forms/DispForm.aspx?ID=4

 

To construct and redirect users to this URL, the View Properties action simply redirects to a custom ASHX handler with the full document URL as a parameter. The handler will in turn lookup the document list item using the SharePoint object model. Once the list item is found, the final redirect URL is created and applied. This happens very fast server side and the user will never notice the extra redirection.

Installing the sample goes like this:

  1. Download and unzip the complete sample.
  2. From the unzipped folder, open the file ActionableSearchResults.xsl in Visual Studio, SharePoint Designer or any other text editor.
  3. Copy and paste the XSLT code from the file to the XSLT editor on the Core Results Web Part. For more information see customizing search results with custom XSLTs in SharePoint Server 2007.
  4. From the sub folder SharePoint Action Redirect, run the script install.cmd to deploy the sharepointredirect.wsp solution to your SharePoint farm.
  5. Done.

Disclaimer: The sample code is provided as-is and you are free to modify it anyway you see fit.

SharePoint Search ,