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

-John