Wednesday, December 17, 2014

Get current logged in username using Client-Side Object Model

ExecuteOrDelayUntilScriptLoaded(getUserName, "sp.js");
var userName='';
function getUserName(){
var clientContext = SP.ClientContext.get_current();
var website = clientContext.get_web();
currentUser = website.get_currentUser();
clientContext.load(website);
clientContext.load(currentUser);
clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}

function onRequestSucceeded() {
userName=currentUser.get_title();
alert(userName);
}

function onRequestFailed(sender, args) {
    alert('Error: ' + args.get_message());
}

No comments:

Post a Comment