SharePoint: Mailto Hyperlink in a DVWP
Author: Eric Alexander, Moderator, Stump the Panel
A post came over on Stump the Panel from paisleygo:
Hello Panel,
I have a "Person or Group" column and I want a simple text link in a dvwp that is a link to send the people listed in that column an email. Is that possible?
thanks for you time
I had done something similar in my configurations of the employee training template for our internal use, so I pointed reference to that as a starter. This was deemed not an acceptable solution as there was jQuery involved. So I decided to put on my XSLT hat and do this strictly in a DVWP like originally asked.
I didn’t have specific requirements so I selected an arbitrary list in my site collection as my data source, the announcements list. I inserted a data view web part onto my page and added the Created By column as a column into the display, this is going to be my person/group column for this example.
When dropped onto the page, SharePoint Designer makes this a nice hyperlinked field showing the users display name. Clicking the chevron in the design view and setting the value to text allows you to get at all the information associated with the user.

Once set to Text, the output will change to this:
<nobr><span><A HREF="/sites/ericrules/_layouts/userdisp.aspx?ID=1">Alexander, Eric</A><img border="0" height="1" width="3" src="/_layouts/images/blank.gif"/><a href='javascript:' onclick='IMNImageOnClick();return false;' class='ms-imnlink'><img name='imnmark' title='' border='0' height='12' width='12' src='/_layouts/images/blank.gif' alt='No presence information' sip='[email protected]' id='imn_923,type=sip'/></a></span></nobr>
Now we can utilize XSLT functions to extract the email address. Marc Anderson has a great series on mastering the Data view web part. In this scenario we will be working with 2 string functions, substring-before and substring-after. This is important because we want the email address which is in the middle of the user information. That code looks like this:
<xsl:value-of select="substring-before(substring-after(@Author, "sip='"), "' id=")" />
What this is saying is find me everything after sip=’ in the @Author field and find me everything before ‘ id= (space included), our email address. The next piece of this puzzle was figuring out how to wrap this all up in a mailto hyperlink. Initially I thought of creating a parameter and passing that into the code but realized that was adding complexity that wasn’t needed and I could simply reuse my substring code above.
<a href="mailto:{substring-before(substring-after(@Author, "sip='"), "' id=")}?subject=This is an email from a DVWP"><xsl:value-of select="substring-before(substring-after(@Author, "sip='"), "' id=")" /></a>
So what this is doing is creating a dynamic mailto hyperlink based on the value of the email address contained within the person/group field, in this case the Created By column. The brackets are needed so that it knows to get the information from the XML. When the page is saved and loaded in the browser, clicking the link opens an Outlook window with that email address and a pre-populated subject.
Author: Eric Alexander, Moderator, Stump the Panel
Eric Alexander is a SharePoint Administrator working in the Higher Education sector. He wears many hats in this role and enjoys leveraging SharePoint to build solutions to solve problems across campus. You can find him on Twitter as PirateEric and lurking in the Stump the Panel forums.
http://feeds.feedburner.com/EricsSharepointAdventures
Eric:
Good stuff! An enhancement to this would be to put the logic into a template that is recursive. This will allow you to show mailto: links for multiple selections in a Person or Group column. I notice that the original requestor wanted “to send the *people* listed in that column an email”.
M.
Nice tip! You could also throw in a xsl:choose node wrapper so you could capture any scenarios that didn’t have a value.
Ya this is a single person only, I thought you were going to expand on the multiple people scenario Marc.