Get User by Loginname or Current user on SharePoint 2013 via Javascript

This morning i need to create a simple script function to get User from SharePoint 2013.

Here is coding as below that already created and i decided to post in here as my backup for next time i could see again as my script reference :

ps : Please remember this only applicable for SP 2013 only because on this code i used get_siteUsers() which this only had on SP 2013, not on SP 2010.

var context;
var web;
var user;

ExecuteOrDelayUntilScriptLoaded(init,’sp.js’);
function init(){

}

function getUser() {
context = new SP.ClientContext.get_current();
web = context.get_web();
if (document.getElementById(“T1″).value!=””)
{
alert(document.getElementById(“T1”).value);
user = web.get_siteUsers().getByLoginName(document.getElementById(“T1″).value);
}else{
user = web.get_currentUser();
}
context.load(user);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),Function.createDelegate(this, this.onFailureMethod));
}

function onSuccessMethod(sender, args){
alert(user.get_title());
alert(user.get_email());
alert(user.get_loginName());
}

function onFaiureMethod(sender, args){
alert(‘request failed’ + args.get_message() + ‘\n’ + args.get_stackTrace());
}

LoginName : <input type=”text” name=”T1″ id=”T1″/><input type=”button” value=”Go” onclick=”javascript:getUser()”/>

 

Happy Sharepoint-Ing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.