Tuesday 23 October 2012

ECMA Script to Get Current User Name in sharepoint list form

ECMA Script to Get Current User Name in sharepoint list Custom  form
Steps:

1. Just Add a content editor webpart in you list form(NewForm.aspx or EditForm.aspx) and put the script below:
2. Add your control id(Using View Source you can get id of that contol)
<script type="text/javascript">

var context = null;
var web = null;
var currentUser = null;
ExecuteOrDelayUntilScriptLoaded(GetUserLoginName, "sp.js");
function GetUserLoginName() {
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(' Name:' + this._currentUser.get_title() + '\n Login:' + this._currentUser.get_loginName());
var user=this._currentUser.get_title();
document.getElementById("Control Id").value=user;
}
function onFaiureMethod(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>


Done!

1 comment: