SharePoint 2013: Get the email address of current logged on user

  ExecuteOrDelayUntilScriptLoaded(getWebUserData, “sp.js”);

        function getWebUserData() { 
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        currentUser = web.get_currentUser();
        currentUser.retrieve(); // with out this the get_title() on success call back will not be initialized
        context.load(web);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), 
             Function.createDelegate(this, this.onFailureMethod));
   }

    function onSuccessMethod(sender, args) {
        var userObject = web.get_currentUser();
        var UserEmail = userObject.get_email();
        console.log(UserEmail);
        }

    function onFailureMethod(sender, args) {
        alert(‘request failed Please contact McFarland.Jeffrey@epa.gov’);
    }

You might need the ExecuteOrDelayUntilScriptLoaded method

To get your Client Object Model code to work you might need to add the ExecuteOrDelayUntilScriptLoaded() method. Without this method you might encounter the following error:

Uncaught TypeError: Cannot read property 'get_current' of undefined
Uncaught TypeError: Cannot read property ‘get_current’ of undefined

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

For instance this Client Object Model code to get the current logged on user will not work without the ExecuteOrDelayUntilScriptLoaded() method.

       context = new SP.ClientContext.get_current();
        web = context.get_web();
        currentUser = web.get_currentUser();
        currentUser.retrieve();
        context.load(web);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), 
             Function.createDelegate(this, this.onFailureMethod));
   }

    function onSuccessMethod(sender, args) {
        var userObject = web.get_currentUser();
        var UserTitle = userObject.get_title();
        alert(UserTitle);
        }

    function onFailureMethod(sender, args) {
        alert(‘request failed ‘);
    }
The new code will look like this:
ExecuteOrDelayUntilScriptLoaded(getWebUserData, “sp.js”);
    var context = null;
    var web = null;
    var currentUser = null; 

        function getWebUserData() { 
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        currentUser = web.get_currentUser();
        currentUser.retrieve();
        context.load(web);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), 
             Function.createDelegate(this, this.onFailureMethod));
   }

    function onSuccessMethod(sender, args) {
        var userObject = web.get_currentUser();
        var UserTitle = userObject.get_title();
        alert(UserTitle);
        }

    function onFailureMethod(sender, args) {
        alert(‘request failed ‘);

    }

Getting the logged on user name using the client object model

If you want to use the SharePoint 2010 client object model you can use the code below:

ExecuteOrDelayUntilScriptLoaded(getWebUserData, “sp.js“);
    var context = null;
    var web = null;
    var currentUser = null; 

        function getWebUserData() { 
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        currentUser = web.get_currentUser();
        currentUser.retrieve();
        context.load(web);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), 
             Function.createDelegate(this, this.onFailureMethod));
   }

    function onSuccessMethod(sender, args) {
        var userObject = web.get_currentUser();
        var UserTitle = userObject.get_title();
        alert(UserTitle);
        }

    function onFailureMethod(sender, args) {
        alert(‘request failed ‘);
    }


Here is a video and a simple way to test it.

If you want to save some time by not having to open SharePoint Designer 2010, you run this code directly from Chrome using the web developer tool. The video below shows you how to do that.

Uncaught TypeError: Cannot read property ‘get_current’ of undefined

Client Object Model SP.ClientContext will not work if you are using minimal.master. If you are using minimal.master and want to use the client object model then you will need to add code that loads “sp.js” or you can just use v4.master.

Uncaught TypeError: Cannot read property 'get_current' of undefined
Chrome error
SP.ClientContext.get_current is null or not an object
IE error