Getting the Current User Name or ID in SharePoint using SharePoint Designer (no code)

A colleague sparked my interest in this today by asking me if getting the current user ID was possible without writing code.  This would be a very trivial task by creating a custom web part but with that comes deployment, maintenance, support, etc.  He had a simple requirement to pass the UserID in the query string and thought it would be overkill to do this using code, and I agreed.  This put me on a mission to find out if this was possible using SharePoint designer.  The usual Internet searches didn't return anything useful so once I figured it out I decided that this needed to be posted somewhere.  So here is how you do it:


Using SharePoint designer open up the Data Source Library Pane

Hover over any library or list, using the drop down select "Show Data"


Next you will want to have SharePoint Designer insert a DataFormWebPart for you.  These last two steps aren't really necessary, you could write all the markup by hand, but I find it is usually easier to have SPD do this for you, and then go back and clean it up, than it is to remember everything that you need and start from scratch.  For this part I selected the Title field and used a Multiple Item View Form, you can select different fields/views as your situation calls for.


Now we can get to what this post was really about, getting the UserName and/or the User ID.  To get the UserName all you need to do is add an XSL parameter.  A little known fact here is that any parameter that is found in the parameter bindings section of the data source element can also be passed to the XSL using an XSL parameter.  To be clear about what I added refer to the screen shot below, I have removed all the irrelevant XSL to make this easier to understand.


The above will show you the what SharePoint display's the user name as.  This was a step in the right direction, but I still needed to actual user ID.  To get this I decided to go the server variable route.  Here is the documentation straight from MSDN on the three variables that are probably of interested for this situation.


The name of the user as it is derived from the authorization header sent by the client, before the user name is mapped to a Windows account. This variable is no different from REMOTE_USER. If you have an authentication filter installed on your Web server that maps incoming users to accounts, use LOGON_USER to view the mapped user name.


The Windows account that the user is impersonating while connected to your Web server. Use REMOTE_USER, UNMAPPED_REMOTE_USER, or AUTH_USER to view the raw user name that is contained in the request header. The only time LOGON_USER holds a different value than these other variables is if you have an authentication filter installed.


The name of the user as it is derived from the authorization header sent by the client, before the user name is mapped to a Windows account. If you have an authentication filter installed on your Web server that maps incoming users to accounts, use LOGON_USER to view the mapped user name.

I decided to use the LOGON_USER variable, but note that you can use any server variable here, your not limited by SPD in any way.  To do this I had to add the server variable to the ParameterBindings as well as the XSL.  The end result of my DataFormWebPart is below.

 <WebPartPages:DataFormWebPart runat="server" 
        Title="Shared Documents" 
        DisplayName="Shared Documents" 
        __markuptype="vsattributemarkup" _
        Width="" __WebPartId="{633D5B43-5675-4363-8F69-9BA87A3776ED}">
        <SharePoint:SPDataSource runat="server" 
    <ParameterBinding Name="ListID" Location="None" DefaultValue="EF49CFE7-C6C4-42D6-8FCB-B10D6CDC0A35"/>
    <ParameterBinding Name="dvt_apos" Location="Postback;Connection"/>
    <ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
    <ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate"/>
    <ParameterBinding Name="LogonUser" Location="ServerVariable(LOGON_USER)"/>
    <datafields>@FileLeafRef,Name (for use in forms);@Title,Title;@Approval,Approval;
    @CollectS,Collect Signatures;@CollectF,Collect Feedback;@ID,ID;@ContentType,Content Type;
    @Created,Created;@Author,Created By;@Modified,Modified;@Editor,Modified By;
    @_CopySource,Copy Source;@CheckoutUser,Checked Out To;@_CheckinComment,Check In Comment;
    @CheckedOutTitle,Checked Out To;@CheckedOutUserId,ID of the User who has the item Checked Out;
    @FileDirRef,Path;@FSObjType,Item Type;@HTML_x0020_File_x0020_Type,HTML File Type;
    @File_x0020_Type,File Type;@IsCheckedoutToLocal,Is Checked out to local;@_SourceUrl,Source Url;
    @_HasCopyDestinations,Has Copy Destinations;@ContentTypeId,Content Type ID;@_ModerationStatus,Approval Status;
    @_UIVersion,UI Version;@Created_x0020_Date,Created;@FileRef,URL Path;@File_x0020_Size,File Size;
    @_UIVersionString,Version;@ParentVersionString,Source Version (Converted Document);
    @ParentLeafName,Source Name (Converted Document);@TemplateUrl,Template Link;</datafields>
<xsl:stylesheet xmlns:x="" 
                exclude-result-prefixes="xsl msxsl ddwrt" 
    <xsl:output method="html" indent="no"/>
    <xsl:decimal-format NaN=""/>
    <xsl:param name="dvt_apos">'</xsl:param>
    <xsl:param name="UserID"/>
    <xsl:param name="LogonUser"/>
    <xsl:variable name="dvt_1_automode">0</xsl:variable>
    <xsl:template match="/">
    <xsl:value-of select="$UserID"/><br/>
    <xsl:value-of select="$LogonUser"/>
    </xsl:stylesheet>    </XSL>
 And the screen shot...



And that does it, you can now show user name or user ID without using any code.  An important side lesson of this is the fact that you can use anything in the parameter binding session in your XSL as well. 


Hope this helps.