How to get logged in username in SharePoint using JavaScript

This JSOM SharePoint tutorial explains, how to get logged in username in sharepoint using javascript. The same javascript code will work to get logged in username using JavaScript in SharePoint 2013/2016 or SharePoint Online.

get logged in username in SharePoint using javascript

We need to put the below code in a script editor web part on a web part page. To edit the page and then from the Ribbon click on INSERT -> Web part and then insert a script editor web part from the Media and content category.

Then put the below code inside the script editor web part.

<input type='button' id='id1' value='Get User Name' onclick="GetLoggedInUserName();"/>

<script type=”text/javascript”>
function GetLoggedInUserName()
{
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args)
{
alert(currentUser.get_loginName());
}

function onQueryFailed(sender, args)
{
alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
}
</script>

You can see the output like below:

get logged in username in SharePoint using JavaScript

You may like following JavaScript SharePoint tutorials:

This tutorial, we learned how to get logged in username in SharePoint using javascript.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>