SharePoint Current Username – Add Username to Page

As an expansion to my reference to using SPServices to grab the current username in SharePoint, here is a simple Javascript example of using the GetCurrentUser function.

The following function obtains the current user name from SPServices and put’s it into a variable called “thisUser”. It then executes a text replace on the anchor text of the hyperlink, adding the username to the end.

Add the references to Jquery and SPServices in the section of your masterpage:

<script language="javascript" type="text/javascript" src="/jQuery%20Libraries/jquery-1.6.1.min.js"></script>
<script language="javascript" type="text/javascript" src="/jQuery%20Libraries/jquery.SPServices-0.6.2.min.js"></script>

Add the following in the section of your masterpage, or in a Content Editor Web Part:

<script language=”Javascript”>
function getUserName()
var thisUser = $().SPServices.SPGetCurrentUser({
	fieldName: "Name",
	debug: false
document.getElementById('myAnchor').href=”” + getUserName() + "/";
document.getElementById('myAnchor').innerHTML=”” + getUserName() + "/";

<a id="myAnchor" href=""></a>

The resulting output HTML for that anchor link would become (if logged in as JoeBlow):

<a id="myAnchor" href=""></a>