Get current user id in SharePoint using javascript

In this jsom tutorial, we will see how to get current user id in sharepoint using javascript. In the same way, we can get current user id in sharepoint online using javascript.

I hope by end of the article, you will get to know how to get current user id in sharepoint 2013 using javascript.

Here, I am explaining how to show current user details using the JavaScript Object Model (JSOM) and jQuery in SharePoint online.

Below is the code to retrieve current user details like display name, user name, email id, and user id using Jsom and jQuery in SharePoint.

Open your SharePoint site and go to the web part page and then Edit the web part page -> Add Web parts. And then add a script editor web part into the SharePoint web part page.

Get current user id in SharePoint using javascript

<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>  

You may like following jsom SharePoint tutorials:

Once you execute the jsom code, it will display the current logged in user display name, email, user id and user login name in SharePoint.

I hope this tutorial helps use to learn:

  • get current user id in sharepoint using javascript
  • get current user id in sharepoint online using javascript
  • how to get current user id in sharepoint 2013 using javascript
  • how to get current user name in sharepoint 2013 using javascript
  • how to get current user login name in sharepoint using javascript
  • how to get current user name in sharepoint 2013 using javascript
Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Rajkiran Swain

Rajkiran is currently working as a SharePoint Consultant in India . Rajkiran having 7+ years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).

  • […] Get Current User Details Using JavaScript Object Model (jsom) and jQuery in SharePoint 2013/2016/Online […]

  • >