Get user email address client side
-
25-12-2020 - |
Question
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());
}
Solution
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
.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange