Partilhar via


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

Comments

  • Anonymous
    June 13, 2004
    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"/>

    with

    <ParameterBinding Name="UserID" Location="CAMLVariable;ServerVariable(REMOTE_ADDR)" DefaultValue="CurrentUserName"/>
    but it still returns the user name.

    How can I make this work?
  • Anonymous
    June 14, 2004
    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.

    -John
  • Anonymous
    July 21, 2004
    The comment has been removed
  • Anonymous
    July 22, 2004
    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?