This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
var loginName = ""; | |
var userid = _spPageContextInfo.userId; | |
GetCurrentUser(); | |
function GetCurrentUser() { | |
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")"; | |
var requestHeaders = { "accept" : "application/json;odata=verbose" }; | |
$.ajax({ | |
url : requestUri, | |
contentType : "application/json;odata=verbose", | |
headers : requestHeaders, | |
success : onSuccess, | |
error : onError | |
}); | |
} | |
function onSuccess(data, request) { | |
var loginName = data.d.Title; | |
// alert("Hello " + loginName); | |
// to set the "hello username" into the page | |
document.getElementById("welcome").innerHTML = loginName; | |
} | |
function onError(error) { | |
alert(error); | |
} | |
</script> |
I used that code for a NewForm.aspx, the goal was to display the current User on top of the form. So I added folllowing HTML after the javascript code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<table width="100%" class="ms-formtable" style="margin-top: 8px;" border="0" cellspacing="0" cellpadding="0"> | |
<tbody> | |
<tr> | |
<td width="103" class="ms-formlabel" nowrap="true" valign="top"><h3 class="ms-standardheader"><nobr>User</nobr></h3></td> | |
<td width="350" class="ms-formbody" valign="top" id="welcome"></td> | |
</tr> | |
</tbody> | |
</table> | |
</html> |
The result will be something like this - just edit the html accordingly:
No comments:
Post a Comment