Howto: Display the Username for the Logged on user on a page

This is one of my favorites.  I want my site to say, “Hello, John Jansen, welcome to this site“ or whatever.  But since you can't author server-side code with SharePoint sites, there is no evident way to do this.  The way I get around this limitation is to code XSLT and make it do what I want.

1. Insert a Data View Web Part based on the Announcements list
2. Click on Data > Conditional Formatting
3. Select any data value and Click on Create
4. Click on Show Content
5. Create a Condition that says: Title EQUALS [Current User] and click OK back to the Design view
6. Type “Hello, welcome to this site“ inside one of the Table cells
7. Switch to Code view
8. Find the following code:
<ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
9. Change this code to this:
<ParameterBinding Name="UserID" Location="CAMLVariable;ServerVariable(LOGON_USER)" DefaultValue="CurrentUserName"/>
10. Scroll up in the code and find “Hello, welcome to this site”
11. Change this code to say this:
“Hello, <xsl:value-of select="$UserID"/> welcome to this site”
12. Save the page

Interesting notes:

First, you will see the user name repeat for each value in the Data View, so you should change the display using Data..Style so that it only shows One record.

Second, the ServerVariable() can be any of the server variables as documented on MSDN.

Third, the reason we created the Conditional Formatting in steps 2 - 5 was to make sure $UserID would be in scope.

Hope this works for ya!


Comments (6)
  1. Jeff Roberts says:

    I’m interested in displaying/capturing the Remote IP address of the client accessing the wss site. Using your example, I replaced the

    <ParameterBinding Name="UserID" Location="CAMLVariable;ServerVariable(LOGON_USER)" DefaultValue="CurrentUserName"/>


    <ParameterBinding Name="UserID" Location="CAMLVariable;ServerVariable(REMOTE_ADDR)" DefaultValue="CurrentUserName"/>

    but it still returns the user name.

    How can I make this work?

  2. John Jansen says:

    This should work for you. I suggest you try removing the default value.

    I just did a quick test and this worked for me as expected.


  3. Jason Dever says:

    I tried this, and inserted the line "Hello, welcome to this site" into the Title field.

    When I added the <xsl:value-of select="$UserID"/> variable, it crapped out on me.

    Here is the error: Any suggestions?

    Announcements | AnnoncesTRUE{9DE3BC8C-E25B-41D8-ADC7-E43AD611C42D}FALSEAnnouncements | Annonces0None@ID,ID;@Title,Title | Titre;FrontPage cannot render the XSLT in this Data View. Try to undo your changes or re-insert the Data View.

    Failed setting processor stylesheet : 0x80004005 : A reference to variable or parameter ‘UserID’ cannot be resolved. The variable or parameter may not be defined, or it may not be in scope.

  4. John Jansen says:

    I believe this is because you skipped step 5 or 6, or maybe because I am not being clear in step 5 or 6. Either way, you do steps 1 – 6 in order to get the variable into Scope and avoid this error.

    To trouble shoot this, make sure of the following:

    In step 5, where I say "[Current User]" make sure you are not typing that in, but rather making that choice from the Drop-down.

    In step 6, where I say "inside on of the table cells" I mean one of the table cells that is NOT one of the column headers.

    Does that help?

Comments are closed.

Skip to main content