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

How to launch a document by clicking a link to the file?

Word: <a href='ms-word:ofe|u|path/to/web/word/document.docx'>Link to document</a> Excel: <a href='ms-excel:o...