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

This entry is part 12 of 21 in the series Unlocking the Mysteries of Data View Web Part XSL Tags

Cross-posted from EndUserSharePoint.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”).

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

clip_image002[1]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.

Series Navigation<< 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 13 – Miscellaneous: String Functions >>

Similar Posts

43 Comments

  1. I want to display the summary or roll-up report using DVWP Something like

    Country- Manufacturing Unit – Department – Total Income (Summary)
    USA- Desktop Mfg-Keyboards-10000
    USA-Desktop Mgg-Monitors – 20000

    this is kind of aggregatet report. please advise, how to achieve this. Now I get the report grouped by fields and the group footer shows total.

    1. Saran:

      It’s a matter of editing the XSL to emit the markup you’d like. I find that the way that SharePoint Designer does grouping is really hard to follow, so I generally just write my own XSL.

      M.

  2. June 2012: The “strange” Mystery of @Author empty appears in Italy. Nothing to do. It’s one of many bugs of Sharepoint that the time didn’t resolved.
    Sharepoint 2010, Issue List, EditForm.aspx, Designer 2010: @Author = NULL !!! So: Impossible to use Sharepoint to implement a simple logic to showing edit field only to issue creator (feature that could be native in a good KM platform).

  3. Mark,

    I am using DV to display list items. I want to display author name and title from a Person & Group field called Author. I am able to display the name which is linked to the profile but how do I obtain the title from this field to display ?

    which displays

    Snow, John

    but I also want to display the title along with the formatted name like

    John Snow
    Web Developer

    Please help. Thanks.

    1. Thanks Mark for providing such good instructions on String Functions I was able to figure it out. I am applying the String Function twice to PubAuthor field – first one to get the linked Name and the next to get the Job Title.

      I wanted to post the string but its not allowing me to post HTML here.

      Thanks again.

  4. Hello marc,

    I am using the DataformWebpart on SharePoint 2010 to display data from multiple document Libraries.

    I have allowed the “Sort & Filter on Headers” thus now I have a problem in filtering from the header of (Modified By) as the filter dropdown results are displayed as html (span class=…).

    I really hope you can help on this as I have used the method discribed above but it didnt helped in the filtered dropdown results.

    The weird thing is that this error does not occur in the xsltlistviewwebpart.

    Thank you,
    DH

    1. DH:

      I think we have to call that a “bug”. You can definitely fix it by diving into the XSL and updating it manually to handle the date correctly, but I don’t have a fix handy for you, unfortunately.

      M.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.