Friday, February 26, 2010

SharePoint 2010 ECMAScript - How to know logged in user information

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.
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
var context = null;
var web = null;
var currentUser = null;
    function getWebUserData() {
        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 userObject = web.get_currentUser();
        alert('User name:' + userObject.get_title() + '\n Login Name:' + userObject.get_loginName());
    }
    function onFailureMethod(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>
So, the above code returns you the user object and displays user name and user login name. You can customize it as you want and please let me know the feedback or questions you have.

13 comments:

vmrao said...

Thanks. Nice Post. Is there any way to retrieve 'Current Date and Time' from 'ECMAScript'. Also, Is it possible to get the URL of the page where the script is running ? If so, can you please post relevant code. Thanks a lot.

Jason Lochan said...

Awesome -- this helped save me a lot of time

digiblue said...

There's a typo - a missing l:

function onFailureMethod(sender, args)

Anonymous said...

Great! Thank you for this, really helped me!

Pritesh Gandhi said...

Thanks a lot

Binu said...

Is it possible to get the Manager Name of the logged in User through ECMAScript or jQuery in SP 2010?

Anonymous said...

Great post - I was wondering, do you know how you could get just the username - as opposed to DOMAIN\User?

I tried using different ways to split, but they do not seem to be working...

Thanks!

Justin
(jgreywolf@lostware.com)

Anonymous said...

Never mind - it is working now ;)

Justin

Anonymous said...

Thanks for sharing your code

Anonymous said...

Thanks so much for this but I am having an issue running this. I am fairly new to coding and am getting an error of: SCRIPT5007: The value of the property 'ExecuteOrDelayUntilScriptLoaded' is null or undefined, not a Function object

What type of file should this be in a SharePoint site? (I am trying to do either a button or a link that will add the domain/username variable to the end of it so to pass to an external application / organization.)

Any guidance would be GREATLY appreciated!

KathZ

Praveen said...

Thanks for the comment KathZ. Please make sure you are using IE for testing purpose. I don't know what browser you are using. There are cases the function is not recognized by browser in some versions of Chrome. Please read complete post here:http://social.technet.microsoft.com/Forums/uk/sharepoint2010programming/thread/f250516c-6bc3-4514-a699-b1243ff1a6d6

Anonymous said...

I would like to put logged in user in a form field in SharePoint 2010 instead of displaying as an alert. How do I do it?

Thanks

Praveen said...

This is simple. In javascript, just assign the field with the user name as
textbox1.value = userObject.get_title();

-Praveen.