Get Current User Account Name, E-Mail using Javascript Client Object Model in SharePoint 2013

Get Current User Account Name, E-Mail in JavaScript Client Object Model
You can use this code in Content Editor/Script editor or anywhere in SharePoint artifacts like Pages, Page Layouts, Master pages, or external JavaScript files.

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getCurrentUser, "sp.js");

var currentUser;

function getCurrentUser(){
var ctx= new SP.ClientContext.get_current();
var web = ctx.get_web();
currentUser = web.get_currentUser();
ctx.load(currentUser);
ctx.executeQueryAsync(onSuccess, onFailure);
}

function onSuccess() {
alert(currentUser.get_title()); // Domain\Account 
alert(currentUser.get_email());
document.getElementById('userLogin').innerHTML = currentUser.get_loginName(); 
}

function onFailure() {
alert('request failed' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

<div>Currently Logged User:
    <span id="userLogin"></span>
</div>

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *