Getting the Current User in SharePoint XSLT
SharePoint offers a couple of ways by which we can obtain the current user for use in a XSLT web part like DataFormWebPart or XsltListViewWebPart. All involve first setting parameters through the ParameterBindings property:
1: <ParameterBindings>
2: <ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
3: <ParameterBinding Name="LogonUser_" Location="WPVariable(_LogonUser_)"/>
4: <ParameterBinding Name="LogonUser" Location="ServerVariable(LOGON_USER)"/>
5: <ParameterBinding Name="AuthUser" Location="ServerVariable(AUTH_USER)"/>
6: <ParameterBinding Name="RemoteUser" Location="ServerVariable(REMOTE_USER)"/>
7: </ParameterBindings>
8: <Xsl>
9: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl asp" xmlns:asp="System.Web.UI.WebControls" xmlns:ddwrt2="urn:frontpage:internal" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
10: <xsl:output method="html" />
11: <xsl:param name="UserID"/>
12: <xsl:param name="LogonUser_"/>
13: <xsl:param name="LogonUser"/>
14: <xsl:param name="AuthUser"/>
15: <xsl:param name="RemoteUser"/>
16: <xsl:template match="/">
17: UserID: <xsl:value-of select="$UserID"/><br/>
18: ID: <xsl:value-of select="ddwrt:UserLookup($UserID, 'ID')" /><br/>
19: EMail: <xsl:value-of select="ddwrt:UserLookup($UserID, 'EMail')" /><br/>
20: Login: <xsl:value-of select="ddwrt:UserLookup($UserID, 'Login')" /><br/>
21: LogonUser_: <xsl:value-of select="$LogonUser_"/><br/>
22: AuthUser: <xsl:value-of select="$AuthUser"/><br/>
23: RemoteUser: <xsl:value-of select="$RemoteUser"/><br/>
24: </xsl:template>
25: </xsl:stylesheet>
26: </Xsl>
Here we see different kinds of parameters:
- CAMLVariable: built in variables UserID and Today;
- WPVariable: returns one of the predefined values for _WPID_ (web part client id), _WPQ_ (web part unique id in page), _WPR_ (web part resources folder full URL), _WPSRR_ (web part resources folder relative URL), _LogonUser_ (server variable LOGON_USER), _WebLocaleId_ (current site locale, as in CultureInfo.LCID);
- ServerVariable: returns one of the HTTP or IIS server- defined variables.
This will return something like this:
UserID: Ricardo Peres
ID: 10
EMail: ricardoperes@domain.com
Login: i:0#.w|DOMAIN\ricardoperes
LogonUser_: 0#.w|DOMAIN\ricardoperes
AuthUser: 0#.w|DOMAIN\ricardoperes
RemoteUser: 0#.w|DOMAIN\ricardoperes
The ddwrt:UserLookup extension function returns a value depending on the second parameter; valid values are Id, Email and Login, and you can easily guess what they are for.
You can find a reference for LOGON_USER, AUTH_USER, REMOTE_USER and UNMAPPED_REMOTE_USER here. In my development server, I get the same value for all variables.
By the way, I posted a more advanced solution, which allows access to any profile properties. You can read it here.