Implementing Actionable Search Results with Microsoft Office SharePoint Server 2007 

Tags: MOSS, Search, Development

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:

image

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:

image

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.

 
Posted by Lars Fastrup on 17-Jun-09
8  Comments  |  Trackback Url  | 121  Link to this post | Bookmark this post with:        
 

Comments


Jamescommented onWednesday, 12-Aug-2009
Hi there, this is great. I've been asked if it is possible to apply a breadcrumb-like trail for each search result. So it would show the name of the library as a hyperlink and also the name of the parent site and each subsequent parent site (also as hyperlinks). Is this even possible? I can see that we could create handlers to take users to the document library and site but to actually put the names of these seems a step too far. Would I be right? James.


Lars Fastrupcommented onFriday, 14-Aug-2009
Hi James, well I think the URL contains the information needed to build a breadcrumb, just replace the '/' character with ' > ' or something to make it look like a breadcrumb trail. One problem is of course if the URL name of sites and lists differ from their display title!


Tim Egglestoncommented onFriday, 28-Aug-2009
Hi Lars, great solution! I've found that the Edit Document link appears for folders in document libraries, which obviously we don't want since you can't edit them. Changing to works nicely.


Sofya Narinskycommented onTuesday, 20-Oct-2009
Thanks so much for posting this!! Saved me a lot of time and nerves. VERY useful and VERY easy to implement!


Joecommented onThursday, 21-Jan-2010
Hi, thanks for posting this. Unfortunately I get this error when trying to open a document with office 2003: The URL 'doclib/test.doc' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. Any idea? BG joe


Lars Fastrupcommented onThursday, 21-Jan-2010
Hi Joe, maybe your index is out of date? That is, your file was deleted since the last incremental crawl. Cheers Lars


Rajesh Kassacommented onMonday, 15-Mar-2010
Hi, Thanks for providing one of the greatest and useful solutions in sharepoint 2007. Currently, i'm facing an error while viewing the properites of certain documents having filenames of type test++.doc, tes++v1.doc etc. It looks like the handler is unable to handle the urls of these filenames. It would be great if you could help me in fixing this. Thanks


Johncommented onFriday, 23-Jul-2010
This worked pretty well, thanks for providing this. I had a question about how this determines' security permissions for users. For example, a document that I have full rights to through group memebership (an AD group) does not allow me to edit the document though it does show the edit document link. It gives me an error about being unable to check the document out. Another example, a document that I have full rights to through group membership, but 'Limited Access' by name, does not show me the 'Edit Document' link. I'd appreciate you input. John

Name:
URL:
Email:
Comments:
CAPTCHA Image Validation