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:
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.
Awesome -- this helped save me a lot of time
There's a typo - a missing l:
function onFailureMethod(sender, args)
Great! Thank you for this, really helped me!
Thanks a lot
Is it possible to get the Manager Name of the logged in User through ECMAScript or jQuery in SP 2010?
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)
Never mind - it is working now ;)
Justin
Thanks for sharing your code
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
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
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
This is simple. In javascript, just assign the field with the user name as
textbox1.value = userObject.get_title();
-Praveen.
Post a Comment