Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, November 29, 2012

Getting the User Name and Login Name of the currently Logged User using ECMA Script

var context = ""; 
var web = ""; 
var currentUser = ""; 
    function getLoggedUser() { 
        context = new SP.ClientContext.get_current(); 
        web = context.get_web(); 
        currentUser = web.get_currentUser(); 
         currentUser.retrieve(); 
        context.load(web); 
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod)); 
    } 
    function onSuccessMethod(sender, args) { 
        var strCurrentLoggedUser = web.get_currentUser(); 
        alert('User name:' + strCurrentLoggedUser .get_title() + '\n Login Name:' + strCurrentLoggedUser .get_loginName()); 
    } 
    function onFailureMethod(sender, args) { 
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); 
    }
   
    ExecuteOrDelayUntilScriptLoaded(getLoggedUser, "sp.js"); 

No comments:

Post a Comment