This question already has an answer here:
- How to get current user with javascript? 4 answers
I am trying to store the username return by the variable username using the code below, but it seems nothing is returned (Username:undefined
pops up), may somebody advise how to make it works?
var username;
siteUrl = 'xxx/xxx';
function getLogonUser() {
var clientContext = new SP.ClientContext(siteUrl);
this.website = clientContext.get_web();
this.currentUser = website.get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(Function.createDelegate(this, this.userQuerySucceeded), Function.createDelegate(this, this.userQueryFailed));
}
function userQuerySucceeded(sender, args) {
this.username = currentUser.get_loginName();
}
function userQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
function showName(){
getLogonUser();
alert('Username:' + this.username);
}