SharePoint Get Current User ID, Name, Email, Display Name Programmatically using JavaScript

If you are working with a SharePoint classic site, you should be familiar with using JavaScript to retrieve the current user’s details programmatically. In this tutorial, I will explain how to get the current user ID, email, username, display name, etc., programmatically using JSOM.

Get the Current User ID in SharePoint using JavaScript

Below is the jsom code, to get current user id in sharepoint using javascript. The below code also displays the SharePoint current user name, email and display name.

You can add this code to a SharePoint script editor web part.

<script type="text/javascript">  
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');  
var currentUser;  
function init(){  
    this.clientContext = new SP.ClientContext.get_current();  
    this.oWeb = clientContext.get_web();  
    currentUser = this.oWeb.get_currentUser();  
    this.clientContext.load(currentUser);  
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));  
}  
  
function onQuerySucceeded() {  
    document.getElementById('userLoginName').innerHTML = currentUser.get_loginName();   
    document.getElementById('userId').innerHTML = currentUser.get_id();  
    document.getElementById('userTitle').innerHTML = currentUser.get_title();  
    document.getElementById('userEmail').innerHTML = currentUser.get_email();  
}  
  
function onQueryFailed(sender, args) {  
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());  
}  
</script>  
<div>Current Logged User:  
    <span id="userLoginName"></span></br>  
    <span id="userId"></span></br>  
    <span id="userTitle"></span></br>  
    <span id="userEmail"></span></br>  
</div>  

Check out How to Get Current User using SharePoint Rest API?

Get Logged in User ID in SharePoint using JavaScript

Let us see how to get the current user details in SharePoint using JavaScript Object Model (JSOM).

In a SharePoint environment, SP.UserProfiles JavaScript is used to get user profiles and user properties in custom solutions for SharePoint and apps for SharePoint.

There are various object supported in SP.userProfile namespace. They are given as below.

  • HashTag
  • HashTagCollection
  • PeopleManager
  • PersonProperties
  • ProfileLoader
  • UserProfile
  • UserProfilePropertiesForUser

In this article, I will show the sample code to get the current logged in user using user profile JavaScript.

<script src="/_layouts/15/SP.Runtime.js"></script>
<script src="/_layouts/15/SP.js"></script>
<script src="/_layouts/15/SP.UserProfiles.js"></script>

(function($){

$(document).ready(function(){
// Ensure that the SP.UserProfiles.js file is loaded.
SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, ‘SP.UserProfiles.js’);
});

var userProfileProperties;

function loadUserData(){

//Get Current Context
var clientContext = new SP.ClientContext.get_current();

// People Manager Instance
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

// current user
userProfileProperties = peopleManager.getMyProperties()

clientContext.load(userProfileProperties);

//Execute the Query.
clientContext.executeQueryAsync(onSuccess, onFail);

}

function onSuccess() {
alert(userProfileProperties.get_displayName());
}

function onFail(sender, args) {
alert(“Error Occured: " + args.get_message());
}
})(jQuery);

This is how to get the current logged-in user’s details like display name, email, etc. using JavaScript in SharePoint.

sharepoint get current user details using javascript

Check out SharePoint REST API Get List Items

Get logged in user details using JavaScript Object Model (JSOM)

By using the JavaScript object model we will be able to retrieve current logged in user’s details.

Here is the complete code:

<script src="http://win-eqalhem27jl:7575/sites/one/SiteAssets/1.11.2.jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var currentUser;
if (SP.ClientContext != null) {
SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.js');
}
else {
SP.SOD.executeFunc('sp.js', null, getCurrentUser);
}
});

function getCurrentUser() {
context = new SP.ClientContext.get_current();
web = context.get_web();
currentUser = web.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(onSuccessMethod, onRequestFail);
}

function onSuccessMethod(sender, args) {
var userID = currentUser.get_id();
var userLoginName = currentUser.get_loginName();
var userName = currentUser.get_title();
currentUserAccount = userLoginName.substring(userLoginName.indexOf("|") + 1);
}

function onRequestFail(sender, args) {
alert('request failed' + args.get_message());
}

</script>

Output:

get logged in user id sharepoint javascript

In this tutorial, I explained how to get the current user’s details using JavaScript in SharePoint. I have shown how to get properties such as username, display name, user id, email, etc.

You may also like the following tutorials:

  • >

    Build a High-Performance Project Management Site in SharePoint Online

    User registration Power Apps canvas app

    DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

    Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

    Power Platform Tutorial FREE PDF Download

    FREE Power Platform Tutorial PDF

    Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…