Wednesday, April 13, 2011

How to get current logged user information using JavaScript ?

This is post which is simple and not really needed. But when I started writing the code in ECMAScript I have faced problems in getting the logged in user information. So, my readers may feel good after see this post and really most the users are looking for this too.
By this time, we have really understood all about ECMAScript Client Object Model and debugging. So, it's easy for us to know it well.
Please find the code below to get the current user information.

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

Now copy & paste this JavaScript code after above mentioned tag :

<script type="text/ecmascript">

        ExecuteOrDelayUntilScriptLoaded(getUserData, "sp.js"); 

    // To Get Current User Name
    var context = null; 
    var web = null; 
    var currentUser = null; 
    var currentUserId=''
    function getUserData() {

            context = new SP.ClientContext.get_current(); 
        web = context.get_web(); 
        currentUser = web.get_currentUser(); 
        context.executeQueryAsync(onSuccessMethod, onFaiureMethodl);
    function onSuccessMethod(sender, args) { 
        var userObject = web.get_currentUser(); 
        alert('User name:' + userObject.get_title() + '\n Login Name:' + userObject.get_loginName()); 

    function onFaiureMethodl(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); 
    //--- END Get Current User Name


Post a Comment