1

I am trying to fetch the login user "Email" with below simple code, but finding no luck. Can any one help me in sorting out this problem.

<script src="https://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/SiteAssets/jquery.SPServices-2014.02.js" type="text/javascript"></script>
<script src="/SiteAssets/jquery.SPServices-2014.02.min.js" type="text/javascript"></script>
<script src="/SiteAssets/spjs-utility.js" type="text/javascript"></script>

<script type="text/javascript">
function getUser
{
context = new SP.ClientContext.get_current(); 

web = context.get_web(); 

this._currentUser = web.get_currentUser(); 

context.load(this._currentUser); 

context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),Function.createDelegate(this, this.onFailureMethod)); 
} 

function onSuccessMethod(sender, args) 
{ 
alert(this._currentUser.get_title()); 
alert(this._currentUser.get_email()); 
} 
function onFaiureMethod(sender, args) 
{ 
alert('request failed' + args.get_message() + '\n' + args.get_stackTrace()); 
} 

</script>
  • what's the error you are getting? – Atish Dipongkor - MVP Oct 9 '16 at 11:57
  • I recommend using an JS IDE for formatting, linting and syntax errors. – Mx. Oct 10 '16 at 9:31
0

Get current logged user details by calling following init() function:

<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(); // 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>
    <span id="userId"></span>
    <span id="userTitle"></span>
    <span id="userEmail"></span>
</div>
0

Your code is not valid that's why you are not getting email and title of the user.

  • () is missing in getUser function.
  • You have not called getUser function. You need to call it after sp.js is loaded.

Modified code

function getUser() {
    context = new SP.ClientContext.get_current();

    web = context.get_web();

    this._currentUser = web.get_currentUser();

    context.load(this._currentUser);

    context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
}

function onSuccessMethod(sender, args) {
    alert(this._currentUser.get_title());
    alert(this._currentUser.get_email());
}

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

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUser);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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