SharePoint 2016: Get Current User Using JavaScript

JSOM In SharePoint

In this post, I will explain How to

Get Current SharePoint User using JavaScript Client Object Model (JSOM).

JavaScript Client Side Object Model  In SharePoint

You may be also interested to read SharePoint 2016: Get Current User Using REST API.


Get Current SharePoint User Using JavaScript (JSOM)

What’s JSOM?

  • The JavaScript object model is presented in SharePoint 2010,
  • It is a collection of SharePoint JS files that executed in the client-side,
  • Those files are deployed in the “LAYOUT” directory of SharePoint.

The JSOM includes a lot of JS files like

  • SP.js.
  • SP.Core.js.

What’s SP.SOD.executeOrDelayUntilScriptLoaded

The SP.SOD.executeOrDelayUntilScriptLoaded has two parameters

  • Function Name.
  • JSOM file name.

This function ensures that the JSOM file is loaded first then execute a specified function in its parameters.

If this function doesn’t help, try to use
SP.SOD.executeFunc(‘sp.js’, ‘SP.ClientContext’, yourfunctionname);

You may be also interested to read SharePoint 2016: JSOM is only working in Edit Mode.

What’s the ClientContext

The client context is a class that used to return information about the current objects: like Current Web, Site Collection, site, user …etc.

Get Current SharePoint User Info Using JSOM

  • Edit your page.
  • Add Script Editor Web Part.
Script Editor Webpart
  • Add the below script.
<script src="/_layouts/15/SP.Runtime.js" type="text/javascript"></script>
<script src="/_layouts/15/SP.js" type="text/javascript"></script>

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(GetCurrentUserInfo,'sp.js');
// if the above function doesn't work try to use the below one
//SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetCurrentUserInfo);
var currentUser;
function GetCurrentUserInfo(){
    var cx = new SP.ClientContext.get_current();
    currentUser = cx.get_web().get_currentUser();
    cx.load(currentUser);
    cx.executeQueryAsync(Function.createDelegate(this,this.onSucceeded), Function.createDelegate(this,this.onFailed));
}

function onSucceeded() {
    alert(currentUser.get_loginName().substring(currentUser.get_loginName().indexOf('|')+1)); 
    alert(currentUser.get_title());
    alert(currentUser.get_email());
}

function onFailed(sender, args) {
    alert('Error: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>

You may be also interested to read SharePoint 2016: Get Current User Using REST API.


Get Current SharePoint User Login Name Using JSOM

You can use the “get_loginName()” to get the current login name using JSOM.

This will return the current login name with the below format

Get Current login Name using javascript in SharePoint
i:0#.w|Domain\username

To get only the “domain\username” format use the below code

currentUser.get_loginName().substring(currentUser.get_loginName().indexOf('|')+1);
Get SharePoint user Login Name Using JSOM

You may be also interested to read This web browser either does not support JavaScript or scripts are being blocked.

Get Current SharePoint User Display Name Using JSOM

You can use the “get_title()” to get the current user display name using JSOM.

Get Current user display Name using javascript in SharePoint

Get Current SharePoint User Email Using JSOM

You can use the “get_email()” to get the current user email using JSOM.

Get Current user email using javascript in SharePoint

Applies To
  • SharePoint 2016.
  • SharePoint 2013.
Conclusion

In this post, I explained how to use the JavaScript Object Model (JSOM) to get the SharePoint Current User details:

  • Login Name.
  • Display Name.
  • Email.
See Also

Leave a Reply

Scroll to Top