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
});
return(thisUser)
}
document.getElementById('myAnchor').href=”http://companyname.com/” + getUserName() + "/";
document.getElementById('myAnchor').innerHTML=”http://companyname.com/” + getUserName() + "/";
</script>

<a id="myAnchor" href="http://companyname.com">http://companyname.com/</a>

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

<a id="myAnchor" href="http://companyname.com/JoeBlow">http://companyname.com/JoeBlow/</a>

Leave a Reply

I've been low-key stressed about how to view the many email inboxes I have in Outlook in one view without having to scroll and hunt for new messages.

The 'macro' method in this video works great.

View Multiple Inboxes at Once in Outlook 365 https://youtu.be/NpFSsovHgVo via @YouTube

We help Department of Defense clients realize benefits such as faster decision making, total traceability, and automated updates by identifying targeted goals and putting an emphasis on a culture of adoption through buy-in by individual team members.

https://bit.ly/3IRc68a

How to: Create a report quickly from a #SharePoint list or library in the #PowerBI service
📈📉📊📈📉📊
https://bit.ly/3vbKfMD
#Microsoft365

Leverage Microsoft Project for the web with the Project Accelerator https://youtu.be/Hxr8dncHmT8 via @YouTube

Load More...