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.

23 comments:

  1. 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.

    ReplyDelete
  2. Awesome -- this helped save me a lot of time

    ReplyDelete
  3. There's a typo - a missing l:

    function onFailureMethod(sender, args)

    ReplyDelete
  4. Great! Thank you for this, really helped me!

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

    ReplyDelete
  6. 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)

    ReplyDelete
  7. Never mind - it is working now ;)

    Justin

    ReplyDelete
  8. Thanks for sharing your code

    ReplyDelete
  9. 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

    ReplyDelete
  10. 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

    ReplyDelete
  11. 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

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

    -Praveen.

    ReplyDelete
    Replies
    1. Hi Praveen I used this and it worked great in Fire Fox for me but IE browser says it is undefined. Any suggestions.

      Delete
  13. Hi,

    I'm using SharePoint 2010 Foundation and I need to create a Hyperlink that includes the current username

    Exemple: http://companyweb/"current username"

    I'm using Content Editor, but I'm don't know the right code to get it working.

    This is the code that I'm using:

    href="/" >Go Home

    Thanks

    ReplyDelete
  14. How to display Department, job title and other contents

    ReplyDelete
  15. Thanks a lot for this, it works great, I got the email information with the script, however I would like to know if it's possible to have that information somehow sent as parameter to a SQL DataSource connection to be able to filter my SELECT command. Sharepoint at my work is kinda blocked and I can't use the profile parameters,any ideas?

    ReplyDelete
  16. Such a GREAT post man!... Thank you so much!... it was really helpful!!!

    ReplyDelete
  17. in a similar way how to get the department of the logged in user.. and also it would be great if u send the parameter(get_????() for department) for getting the dept

    ReplyDelete
  18. how to get the department of the current logged in user

    ReplyDelete
  19. Azhar,
    Please take a look at this article. It has the answer: http://msdn.microsoft.com/en-us/library/office/jj920104(v=office.15).aspx

    ReplyDelete
    Replies
    1. Praveen

      i mean to ask u to get the title u have used userObject.get_title() rite for getting the dept what should i use...

      Delete
  20. How to know whether the current logged in user is in which AD group.

    ReplyDelete