SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var user = web.get_currentUser();

I used above code to get the current user using JavaScript, but I got an error:

'SP.ClientContext.get_current' is null or not an object

What should I do? Any help appreciated.

share|improve this question
Which version of SharePoint are you using and what type of solution are you building? – EHorodyski Jul 18 '13 at 14:59
i am using SharePoint 2013. – Nera Jul 18 '13 at 17:50

I have used SharePoint’s JavaScript Object Model to retrieve the current logged in username.Once you have executed the load and executeQueryAsync methods and the call to SharePoint was sucessful, then inside of the onQuerySucceeded method is where I display the username to the browser using the SP.Principal.get_loginName property. Try it out…you can host and run the code below inside of a content editor web part in SharePoint 2010.

<script type="text/javascript">
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_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());
<div>Current Logged User:
    <span id="userLoginName"></span>
    <span id="userId"></span>
    <span id="userTitle"></span>
    <span id="userEmail"></span>

or use the below code

function CallClientOM()
var context = new SP.ClientContext.get_current(); = context.get_web();
this.currentUser = website.get_currentUser();
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

function onQuerySucceeded(sender, args)

function onQueryFailed(sender, args)
alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
share|improve this answer
i tried your code, but got an error it showed "Sys.ArgumentUndefinedException: Value cannot be undefined. Parameter name: method" – Nera Jul 18 '13 at 18:22
@Karthik-Jaganathan: This is exactly what I needed. Are there other properties I can get? Like department and phone number? – IMTheNachoMan Nov 19 '15 at 0:54

I normally use this function below. You can modify it depending on how you want to use it.

function getUserInformation(){
   var context = new SP.ClientContext.get_current();
   var web = context.get_web();  
   var currentUser = web.get_currentUser();  
      function(){ //On success function
        var userObject = web.get_currentUser();
        var email = userObject.get_email();
        var id = userObject.get_id();
        var loginName = userObject.get_loginName();

     function(){ //On fail function
        alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
share|improve this answer

I have used below JavaScript function to get login name of current logged in user.

While working on this code, it was noticed that get_loginName() function brought i:0#.w|DomainName\asad. So I have used string manipulation to get the desired one i.e. DomainName\asad

function getCurrentUser() {
    try {
        var clientContext = new SP.ClientContext.get_current();
        var tempcurrentUser = clientContext.get_web().get_currentUser();
        clientContext.executeQueryAsync(function() {

            //If you notice tempcurrentUser will contain 
            //something like this i:0#.w|DomainName\asad. 
            //But we would want only DomainName\asad, so we use string manipulation

            var index = tempcurrentUser.get_loginName().indexOf('|') + 1;
            var currentUser = tempcurrentUser.get_loginName().substring(index);

            // Here you will get the desired login name 
            // of current logged in user i.e. DomainName\asad

        }, Fail);
    } catch (err) {

function Fail() {
share|improve this answer

You can get logged in user name/id by using

  • _spPageContextInfo.userId
  • _spPageContextInfo.userLoginName

Hope it helps someone.

share|improve this answer
AFAIK _spPageContextInfo.userLoginName is available in SharePoint Online, and not in On Premise. – Asad Refai Nov 18 '15 at 5:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.