1

I am using the following code (below) in a IE 11 browser on SharePoint 2013 on premises to successfully to return the user and email address - however I am unable to get the email to pass to a variable to use for a later third party script.

Please can someone advise how pass the email out to a variable - All the usual methods leave the variable undefined?

var clientContext;
var user;

// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

function sharePointReady() {
    clientContext = SP.ClientContext.get_current();
    user = clientContext.get_web().get_currentUser();

    clientContext.load(user);
    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() {
    alert('The email address of the current user is ' + user.get_email());
    alert('The account name is ' + user.get_loginName());
}
function onQueryFailed(sender, args) {
    alert('Error: ' + args.get_message());
}
  • THis code was lifted from msdn.microsoft.com/en-us/library/office/jj245596.aspx – Craig Jun 24 '17 at 9:07
  • Have you checked what is in the global _spPageContextInfo.... – Danny '365CSI' Engelman Jun 24 '17 at 17:26
  • Ok my bad - I now appreciate the call was asynchronous and for my subsequent code to run correctly it needed to be moved into the onQuerySucceeded. Once I did this the email address was resolved and available for the next part of my script. I have marked Dmitry as correct as he had correctly diagnosed this in the second part of his response. (Thanks Dmitry). Thanks everyone for help Craig ... – Craig Jul 27 '17 at 14:39
0

Is it just email that is null? If so, it might be unset/unavailable.

Is it possible, that you access user variable before it initialized?

sharePointReady, and onQuerySucceeded are async. Probably you should add you code in the end of onQuerySucceeded.

  • Thanks for your help. Yes the async is the problem in this case. I have script running after and it runs before the onQuerySucceeded has run and set the variable for the user email address. Trying to see if I can include the third party script with in the success module and then have it run once the variable set correctly. Will mark correct once i prove this in code. – Craig Jun 24 '17 at 20:00
  • You should try to use promises/ defferds to fix this issue. – Nadeem Yousuf Jun 25 '17 at 7:54

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.