Monday, 25 June 2012

Get Current User Login Name,Login ID,Email in Office 365 SharePoint Online


In SharePoint Online you can get user login name and email using the ECMASCRIPT Client Object model but the Login ID is something that you do not get that easily.

User Login ID - All user's in SharePoint Online has login ID preceded by "i:0#.f|membership" so if you try to get this._currentUser.get_loginName(); it will give you somthing like i:0#.f|membership|user@email.onmicrosoft.com

Microsoft says the behavior was by design so we cannot do anything.

Here is a script to get Current User Login Name and Email.

function getUser
{
context = new SP.ClientContext.get_current();

web = context.get_web();

this._currentUser = web.get_currentUser();

context.load(this._currentUser);

context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),Function.createDelegate(this, this.onFailureMethod));
}

function onSuccessMethod(sender, args)
{
alert(this._currentUser.get_title());
alert(this._currentUser.get_email());
}
function onFaiureMethod(sender, args)
{
alert('request failed' + args.get_message() + '\n' + args.get_stackTrace());
}

Ads by Google

No comments:

Post a Comment