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='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”).

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

43 Comments

  1. Hi … thanks for ur post.
    I am facing one issue. I am trying to display the Author field in the itemStyle.xsl
    this is how i do –

    but when it gets rendered, it is not rendering as a link. But just a plain text.
    I need the href to userdisp.aspx also.

    Is there something i am missing out?

    hope to get some help
    Bhargavi

    Reply
    • Bhargavi:

      Your code didn’t come through, but you’ll need to do something like this (if I understand your question correctly):

      <xsl:value-of select="@Author" disable-output-escaping="yes"/>
      

      M.

      Reply
  2. Hi Marc,

    great series!
    I’ve got one question, though.

    You say:”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”.

    Could you elaborate on where to define the variable? I have tried myself and most of the time, trying to sort makes my webpart crash.

    Thanx!
    Jack

    Reply
    • Anonymous:

      Typically you’ll define the variables at the top of the XSL template where you want to use it. However, it sounds like you’d like to use the value to sort?

      M.

      Reply
  3. Marc,
    This has really helped me get closer to filtering on a user or group column; however, I believe I am missing something (and this is probably due to unfamiliarity with the xsl): I can create the variable to hold the ID of my person/group column (mine is called ‘username’) but how do I add a filter that compares the Current User’s ID# to the variable created? I’ve tried several ways of making the comparison but it seems to fail by either incorrect data or just fun xsl errors. Could you point me in the right direction?

    Reply
    • Brendan:

      The answer is probably to build the parsing out of the userID or whatever into the filer on the definition of the rowset. Something like this:

      <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[substring-before(substring-after(@Author, ‘ID=’), ‘&quot;’) = $UserID]"/>

      M.

      Reply
      • Hey Marc,
        This does help with the filtering; but I fear that the value being obtained automatically by SharePoint (which it stores in $UserID) isn’t a number; rather, it seems like it’s the username. Do I need to use your services library to retrieve the actual ID number of the current user to then make the comparison to the ID number of the person/group field?

        Reply
          • Absolutely, but I think the information is acting up slightly because this is a linked data source instead of just a standard list. No results return when I try it and I think it’s because, when I just display the value of the $UserID parameter that is auto-generated; it’s not a number…is that simply a display thing?

            Reply
            • Hey Marc,
              I think I figured it out finally: In a simple sharepoint list; you should be able to actually add a filter via the little dvwp chevron and just say “x person or group column” “equals” “[Current User]”. The behavior of doing this is different when using a linked data source. In a linked data source, it attempts to do what you mentioned, except it just tries to normalize-space the @Author field. In a single, simple list; it actually puts the CAML into the selectcommand at the top. So, I had to find the part of the xsl where it is pulling the particular list and copy-paste the CAML into the selectcommand and it worked like a charm.

              Reply
  4. PLEASE HELP!

    For me renders as null in both designer and the browser. I don’t understand why there is nothing there! I am in the exact same situation with a custom EditForm and can’t pull through the author field!

    PLEASE HELP!

    Reply
      • Hey, thanks for replying.

        This may be way to much information but thanks for helping so I’m going to put as much as I can.

        I have a custom edit form, and I need to have certain sections of the form only visible to the creator of the item. I tried the following if statement:

        Content

        This didn’t work, and I know that it’s not a simple as that, you do have to put some sort of contains string before @Author that I can’t remember right now. But regardless, as a test I just put this in:

        This is exactly as described in your blog post above, but it returns nothing. That’s basically the only issue: that the value of “@Author” is returned as null.

        If I, instead of using an xsl:value control but instead write:

        this returns a linked username exactly as described in the blog post. However there is no way I can capture that value and use it in an xsl if statement. I have tried putting the above control between param and variable controls and it does nothing.

        Any help you can give with the above, or an alternative way of doing what I am trying to do would be really helpful; this is driving me crazy!

        Thanks.

        Reply
      • Hi, thanks for replying.

        I basically have the exact same problem as this guy here:

        http://social.msdn.microsoft.com/Forums/en-SG/sharepointcustomization/thread/22bed1da-eafa-4fd5-8059-c1ce1c851134?prof=required&ppud=4

        You said that the @Author value is there for you to grab, and he says that his value is null. You said that the value may return as null in designer but not in the browser. However I can’t get it to return anything in the browser.

        I basically have a section of an editform that I have to display to only the creator, so I use Content. I know there should be a contains substring there but I can’t remember that right now.

        That didn’t work so I tested it with . This just returns NOTHING. Null.

        If I write , it does return a linked name, but I can’t find any way to feed this value into the xsl so that I can use it in the if statement.

        Any help with this, or another way of doing it if this can’t be done, would be brilliant.

        Reply
        • Are you sure this is an EditForm and not a NewForm? As I said in that old post, in a NewForm the @Author doesn’t exist yet. Are you working in SharePoint Designer? Visual Studio? If it’s the former, look at the list in the Data Source Library and see what columns are there and populated.

          M.

          Reply
          • Yes it is definitely 100% an EditForm.

            I have checed the Data Source LIbrary in the Data View properties of the EditForm and there is no field called Author or Created By in the datasource; that might be the problem. How do I get the field in there; I can’t understand why it isn’t?

            Thanks.

            Reply
            • It’s surprising that @Author isn’t there. Is this DataSource a SharePoint list or something else? The image below shows you what you ought to see. In this case, I’m looking at an Announcements list.

              M.
              Data Source Library

              Reply
              • It’s a SharePoint datasource. I found it odd as well. The datasource I see is basically exactly like that one (except with my custom fields in it, obviously), but there is no Modified, Modified By, Created or Created By.

                Just so you know, I got round the problem in the end by using my own custom field and passing the author value into it with a calculated field.

                As I said originally, using a SharePoint:FieldValue was able to grab the value, but for some reason it didn’t exist in the Xsl. I was also using a SharePoint:FormField to let people actually edit the created by value so that “Edit:Only their own” wouldn’t be tied down to a single person, and that was working fine. None of that would have caused the author to be removed from the datasource or anything would it?

                Reply
                • Clark:

                  Not seeing the Modified, Modified By, Created or Created By columns ought to be impossible. Those are SharePoint-generated and cannot be removed. The only other thing I can think of is some sort of weird permissions thing, but I can’t imagine how the permissions could be set up to make this happen, either.

                  You *can’t* edit the @Author (Created By) column, so I have no idea how you made that happen, either. Are you absolutely certain that we’re dealing with a SharePoint list as the DataSource here?

                  M.

                  Reply
                  • I have the exact same issue. Except I can see Created & Modified, but I cannot see Created By or Modified By and a couple other custom fields. They appear to be brought in, but then don’t appear in the “Row” columns and therefore can’t be included in formulas/tables. Any clues on why this happens?

                    Thanks, Mike

                    Reply
                    • Mike:

                      Sorry, but I have no idea and I’ve never seen it happening. The only thing I can think of is some custom underlying permissioning that’s on top of SharePoint’s model.

                      M.

  5. Hi,

    Is there way for to get a dataview web part to look like a standard webpart? Things like a gradient background to the title and boarders, as well as making the title of the dataview web part a link to the list, like in a standard added web part.

    Thanks in advance for any help!
    Dave

    Reply
    • Dave:

      By applying the same CSS and markeup patterns, you can match anything! The one thing you might need to do a little strangely is to emit the DVWP title in your XSL rather than having SharePoint do it so that it can be a link. I’ve done all this many times, so it’s absolutely doable. My goal is almost always to use the same markup and CSS classes in my DVWPs so that they are as indistinguishable from things like LVWPs as possible. Thie ensures that any changes to the CSS have the same effect on my DVWPs as any other Web Part.

      M.

      Reply
  6. Hi Marc

    Thanks for your informative blog. I have two issue in this regard where I am stuck:

    1. Numbers shown instead of name
    I am using a name picker column where users can add persons. However, when used in a data view web part, this field does not show the but only the ID number instead:

    59135913

    Any idea why only ID is resolved and not the user name?

    2. Using variable in case of more than one entry
    Tried to work with the variable as suggested in your blog (to avoid showing the link) and it works fine as long as there is only one name entered in the field. As I am using a name picker field, users can add there more than just one name. Here again I am stuck, I do not know how I can show the User Info for more than one entry in this field. If I use your method, only one User (Number…) is shown.

    Maybe you have some tips, would be great and for sure very helpful.

    Thanks & best regards

    Gian

    Reply
    • Gian:

      Sorry for the delayed reply.
      1) Generally, a user is represented by a chunk of HTML which ends up rendering as their name as a hyperlink to their profile page or My Site page. I’m not sure why you’d be seeing just the number unless you’ve edited the way the user is rendered somehow.
      2) Check out the templates at my SPXSLT Codeplex Project and you should be able to see how you can deal with getting multiple values using recursive templates.

      M.

      Reply
  7. I have a submitter column of type “Person or Group”. In the list, i have many submitters in the column. In the dataview am only able to get the first submitter from the column. Please help

    Reply
  8. Hello Marc,

    Thanks for the post. I’ve created a dataview grouped by the created by field. The group by actually seems to be working correctly, however when I reference the nodeset to obtain a count value to append to the group by header, the value of zero is always returned. Any thoughts as to why this is happening?

    Thanks,
    Christina

    Reply
      • Marc,

        I’m a bit confused. I don’t understand where to add a reference to the UserID variable so that it groups by that and not by the @Author field.
        Can this be done within the DVWP console?

        Thanks,

        Christina

        Reply
  9. I get how to use the $UserID field, but it concerns me… the $UserID is just the SharePoint display name… is this really unique? Couldn’t there be two John Does that have the same display name but different accounts? If so, this $UserID method would return items for both users, even though they are actually different accounts….

    I was hoping to use the server variable LOGON_USER which is unique, but that value is not stored in the person or group field, so i can’t compare the two.

    Reply

Have a thought or opinion?