Subscribe: SharePoint SharePoint SharePoint

Saturday, July 6, 2013

How to get current user displayname with javascript in sharepoint ?

If you need to retrieve the name of the current logged in user in sharepoint , it can be easily done with the help of a ECMA script. Following code illustrates the same : 

<script type=”text/javascript”>
ExecuteOrDelayUntilScriptLoaded(getDisplayName,”sp.js”);
var currentUser;
function getDisplayName(){
this.clientContext = new SP.ClientContext.get_current();
this.oWeb = clientContext.get_web();
currentUser = this.oWeb.get_currentUser();
this.clientContext.load(currentUser);
this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded() {
alert( currentUser.get_loginName());
}

function onQueryFailed(sender, args) {
alert(‘Request failed. \nError: ‘ + args.get_message() + ‘\nStackTrace: ‘ + args.get_stackTrace());
}
< /script>

0 comments:

Post a Comment