Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns
Author: Marc D. Anderson
http://mdasblog.wordpress.com
As I got into writing this “last” article in the series, I realized that between the things people have asked me over the course of the series and the things I think might be useful, I have far more than one more article to write. I think I’ll keep this series going for a while (Mark willing) and try to cover more of the things that you can do in Data View Web Parts (DVWPs), XSL-based or not.
Person or Group columns are pretty unique in that they contain a big blob of markup which represents a sort of “person object”. They look like a big mess, but they actually contain a great deal of information. Here’s what a typical Person or Group column might contain.
<xsl:value-of select="@Author"/>
renders something like
<nobr><span><A HREF="/_layouts/userdisp.aspx?ID=15">Rufus T. Farnsworth</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_1,type=smtp'/></a></span></nobr>
There will be some variation based on whether you’ve got presence turned on, what type of authentication you are using, etc., but as you can see, it’s messy. Since the column value contains markup, when you display it using the <xsl:value> tag, you should also use the disable-output-escaping=”yes” attribute.
<xsl:value-of select="@Author" disable-output-escaping=”yes”/>
renders as
Rufus T. Farnsworth
which is a link to the user profile page (_layouts/userdisp.aspx) for this user.
Even worse, if the same user is displayed multiple times in the DVWP, the markup won’t usually even match! Here’s the same user displayed from a different item in the same list:
<nobr><span><A HREF="/_layouts/userdisp.aspx?ID=15">Rufus T. Farnsworth</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_4,type=smtp'/></a></span></nobr>
As you can see, id=’imn_1,type=smtp’ and id=’imn_4,type=smtp’ don’t match. So if you try to do sorting or grouping by a Person or Group column, you get “unexpected” results. (Most of us just call this the “wrong answer”).
One way to deal with the variations in the Person or Group column values is to substring out the user’s unique ID and use that for your sorting or grouping column:
<xsl:variable name=”UserID” select="substring-before(substring-after(@Author, ‘ID=’), ‘"’)"/>
The variable $UserID will now contain 15 for both versions of this same user.
If you don’t want to display the user’s name as a hyperlink, you can do the same sort of substring trick. Simply substring out the actual name:
<xsl:variable name="UserAccount" select="substring-after(substring-before(substring-after(@Author, ‘ID=’), ‘<’), ‘>’)"/>
In this example, the variable $UserAccount = "Rufus T. Farnsworth".
I’ve been asked many, many questions about Person or Group columns over the years, but these two tips address the majority of the questions.
I’ve used some of the XSL string functions above, and those functions can be really useful in many other ways as well. In my next article, I’ll go over what string functions are available and how you might use them in your DVWP’s XSL.
Author: Marc D. Anderson
http://mdasblog.wordpress.com
Marc D. Anderson is a Co-Founder and the President of Sympraxis Consulting LLC, based in Newton, MA. He has over 25 years of experience as a technology consultant and line manager across a wide spectrum of industries and organizational sizes. Marc has done extensive consulting on knowledge management and collaboration and what makes them actually work in practice. Marc is a very frequent “answerer” on the MSDN SharePoint – Design and Customization forum.
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 1: Overview
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 2: xsl:template
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 3: xsl:call-template
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 4: xsl:with-param
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 5: xsl:param
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 6: xsl:variable
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 7: xsl:for-each
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 8: xsl:sort
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 9: xsl:if
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 10: xsl:choose
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 11: xsl:value-of
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 12: Miscellaneous - Person or Group Columns
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 13: Miscellaneous - String Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14: Miscellaneous – ddwrt Namespace Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15: Miscellaneous – Field / Node Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 16: Miscellaneous – xsl:attribute
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 17: Miscellaneous – xsl:comment and xsl:text
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18: Miscellaneous – Some Math / Number Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19: Miscellaneous – More Math / Number Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 20: xsl:import
- EUSP eBook Store: First SharePoint Title is Now Available
Marc,
To perform the ’substring’ operation on the variables, you obvioulsy have to know what the initial variable ‘looks like’. What is your prefered/favorite way to do that?
I am kind of stumped on the ‘"’ ‘<’ and ‘>’ expressions.
Greg
OK so looking at my post above, I am not so stumped anymore…
But my question about fav. way to quickly return how the variable how the variable loooks like to better dissect it remains.
THanks,
Greg
Greg:
In this case, just displaying the Person or Group column without the
disable-output-escaping=”yes"
attribute will show you what the values are in SharePoint Designer. I always work in Split mode so that I can see both the code I’m writing (in the top pane) and the rendering (in the bottom pane). Split Mode is also the best way to work if you want to learn what effect changes you make in the dialogs have on the XSL which Designer generates.M.
Just want to say, that the substring method only works, if you don’t allow multiple values in the person or group field. In this case you need a recursive template (code example: http://blog-sharepoint.blogspot.com/2009/03/sharepoint-xsl-string-replacement.html).
Right you are, Nicole. Thanks for posting the link. I’ve got some recursive templates like this on my blog as well. In this post, I wanted to keep it simple. ;+)
M.
I thought I mention it, because this confused me when I first tried to use the simple substring method with a multiple field and only the first person showed up ;o)
So could you turn @Author into a nodeset and then use xpath on that to get the $UserAccount and $UserID?
like
Brian:
Huh. Never thought of that! I suppose you could.
M.
Hi Marc,
Great Article..!! It helped me a lot
Is there a way to display image from person type of column in dataview. In list view if we take “Name with Picture” option for person type of column, it shows both name and picture. But on convering to XSLT it shows only name as a hyperlink but no image.
Himanshu:
You’d need to look at the chunk of HTML to see where the right info is to build the link to the picture. I’m sure it’s doable.
M.