Wednesday, August 01, 2007

Modifying Search to Add a Send Link By Email link

My customer wanted a "send link by email" next to each search result item. "Easy", I thought and went ahead to edit the xslt for the search results. But I found out a problem with links that had spaces in them - the link in outlook would appear broken. To solve this, I had to write some simple javascript, and the process is described below.


If you want a mailto link (href="mailto") that has in the body a link to a file, you may get a link like this:

href="mailto:body=http://server/site/library/this is my file.doc"

This is a problem, because the file name contains spaces. The email will open with a broken link in it's body:


Luckily, SharePoint has a javascript functions that solves that for us. They are "escapeProperly" and "navigateMailToLink". What I did is create a function of my own in the page:

function SendEmailWithLink(link)
var link = "mailto:?body=" + escapeProperly(link);
return false;

And my link is:

href="#nowhere" onclick="javascript:SendEmailWithLink('');"

This article would'nt be complete if I didn't explain why I did this - this if for the search results xslt - the idea was to add a "send link by email" button next to each search result. The solution - change the xslt to include the script above, and a button like this:

<a title="E-mail a Link" href="#nowhere">
<xsl:attribute name="onclick">
javascript:SendEmailWithLink('<xsl:value-of select="$url"></xsl:value-of>');
<img border="0" src="/_layouts/images/ICMSG.GIF"/> 

We also added other actions (I will try to get them published) and came up with the following for each result item:


Anonymous said...

Thanks was helpfull

Anonymous said...

usefull work!

Anonymous said...

Thanks, this is a great tip: we have in mind creating something very much like your "Action" opitions on our search results.

Anonymous said...

Hi, I'm trying to get this working for SP2010 and I keep getting the "mailto:?body=" included with the url in the body of the email. I might be doing something wrong, I'm not sure.
Does the javascript code go in with the XSLT core search results?
If you have time, are you able to update the code for SP2010?
Love your work