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(); 
        currentUser.retrieve(); 
        context.load(web);
        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

</script>


Comments

Mohit Gupta said…
i tried to implement this code, but problem is if instead of alerting it i try to document.write te username it writes off the entire page code with the login name?
Anonymous said…
Thank you so much worked perfectly.

@Mohit Gupta: If you're just wanting to display the username on a page I would recommend writing to a tag:

//outside of Script, had to use [] instead of <>
[a id="test99"][/a]

//in place of alert
document.getElementById("test99").innerHTML = userObject.get_title();
Anonymous said…
Thank you for this code! It saved me a lot of trouble on a SharePoint project. Using this code, I was able to send the name of the current user to an external program displayed in an iframe on a SharePoint ASPX page.
Anonymous said…
Hi

What about the extended properties of the User? This code gets the Title, and LoginName - and works well enough - thanks.

But, what about Picture, Notes, etc?

Thank you kindly
Anonymous said…
This is a great post. I tried to apply this in SP2010 and works perfectly as i want.
But now when am trying to apply this in MOSS2007 it seems to be not working. The page loads fine without any error. But the alert is not showing up.
Could you please let me know how do i implement this in MOSS 2007 as well?
Many thanks in Advance.
Anonymous said…
When I posted that code

I am getting this error can you please look into this

An error occurred during the processing of /sites/testing/hrr/SitePages/Home.aspx. Only Content controls are allowed directly in a content page that contains Content controls.
Anonymous said…
I am getting this Error

An error occurred during the processing of /sites/testing/hrr/SitePages/Home.aspx. Only Content controls are allowed directly in a content page that contains Content controls.
Anonymous said…
Thanks a lot for this great post.
My question, how can I get all the methods for the "Get Current User". I am looking to populate another information like "Email" and "Phone Number".

Thanks again,
Sam
Anonymous said…
All,

How can I get all the methods for the "Get Current user" object? I am looking to get more information such as "Phone number", "email"

Thanks All,
Sam
Gaurav Goyal said…
Hi,
You can get all properties of user. Please have a look to this link :
http://msdn.microsoft.com/en-us/library/hh185012.aspx

Regards
Gaurav
Anonymous said…
Thanks a lot,

What if I need to use this method to set the outputs valuse like "Name", "Email" as a defulat values in the NewForm.aspx page, what should I do?

Sam

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Change Language for current user with JSOM in SharePoint Online

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

SharePoint 2010 CSS Chart