Supporters of End User
Web

Unlocking the Mysteries of Data View Web Part XSL Tags - Part 12: Miscellaneous - Person or Group Columns

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.
Categories:MOSS; WSS; 2007; 2010; SharePoint Designer; Site Manager/Power User; Unlocking the Mysteries of the Data View Web Part; Data View Web Part; Templates

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='rfarnsworth@bigco.com' 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='rfarnsworth@bigco.com' 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=’), ‘&quot;’)"/>

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=’), ‘&lt;’), ‘&gt;’)"/>

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.

Comments

Greg

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

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 '"' '' expressions.
Greg

Posted 02-Mar-2010 by Greg
Greg

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

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

Posted 02-Mar-2010 by Greg
Marc D Anderson

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

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.

Posted 03-Mar-2010 by Marc D Anderson
Nicole

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

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).

Posted 03-Mar-2010 by Nicole
Marc D Anderson

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

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.

Posted 03-Mar-2010 by Marc D Anderson
Nicole

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

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)

Posted 03-Mar-2010 by Nicole
Brian Bedard

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

So could you turn @Author into a nodeset and then use xpath on that to get the $UserAccount and $UserID?

like


Posted 06-Mar-2010 by Brian Bedard
Marc D Anderson

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

Brian:

Huh. Never thought of that! I suppose you could.

M.

Posted 06-Mar-2010 by Marc D Anderson
Himanshu

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

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.

Posted 30-Aug-2010 by Himanshu
Marc Anderson

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns

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.

Posted 30-Aug-2010 by Marc Anderson

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: