1,421 articles and 9,817 comments as of Thursday, March 4th, 2010

Tuesday, March 2, 2010

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=’), ‘&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.

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.

 

Please Join the Discussion

6 Responses to “Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12: Miscellaneous – Person or Group Columns”
  1. Greg says:

    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

  2. Greg says:

    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

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

  4. Nicole says:

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

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

  6. Nicole says:

    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)


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!