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.
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.
ReplyDeleteAwesome -- this helped save me a lot of time
ReplyDeleteThere's a typo - a missing l:
ReplyDeletefunction onFailureMethod(sender, args)
Great! Thank you for this, really helped me!
ReplyDeleteIs it possible to get the Manager Name of the logged in User through ECMAScript or jQuery in SP 2010?
ReplyDeleteGreat post - I was wondering, do you know how you could get just the username - as opposed to DOMAIN\User?
ReplyDeleteI tried using different ways to split, but they do not seem to be working...
Thanks!
Justin
(jgreywolf@lostware.com)
Never mind - it is working now ;)
ReplyDeleteJustin
Thanks for sharing your code
ReplyDeleteThanks 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
ReplyDeleteWhat 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
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
ReplyDeleteI 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?
ReplyDeleteThanks
This is simple. In javascript, just assign the field with the user name as
ReplyDeletetextbox1.value = userObject.get_title();
-Praveen.
Hi Praveen I used this and it worked great in Fire Fox for me but IE browser says it is undefined. Any suggestions.
DeleteHi,
ReplyDeleteI'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
How to display Department, job title and other contents
ReplyDeleteThanks 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?
ReplyDeleteGreat post - thx!
ReplyDeleteSuch a GREAT post man!... Thank you so much!... it was really helpful!!!
ReplyDeletein 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
ReplyDeletehow to get the department of the current logged in user
ReplyDeleteAzhar,
ReplyDeletePlease take a look at this article. It has the answer: http://msdn.microsoft.com/en-us/library/office/jj920104(v=office.15).aspx
Praveen
Deletei mean to ask u to get the title u have used userObject.get_title() rite for getting the dept what should i use...
How to know whether the current logged in user is in which AD group.
ReplyDelete