ECMA Script : To get current logged in user information from User Profile Service Application
ECMA Script to get current logged in user information from User Profile Service Application using "SP.UserProfiles" script files.
<!--script src="/_layouts/15/Scripts/MicrosoftAjax.js" type="text/javascript"></script-->
<!--script src="/_layouts/15/init.js" type="text/javascript"></script-->
<script src="/_layouts/15/sp.runtime.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.js" type="text/javascript"></script>
<script src="/_layouts/15/SP.UserProfiles.js" type="text/javascript"></script>
<script type="text/javascript">
//$(document).ready(function(){
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
//});
var userProfileProperties;
function getUserProperties() {
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
userProfileProperties = peopleManager.getMyProperties();
clientContext.load(userProfileProperties);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}
// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
var messageText = "<b>";
var txtEmpName;
var txtTitle;
var txtDept ;
var txtEmail ;
var txtEmpId ;
if (userProfileProperties.get_userProfileProperties()['PreferredName'] != "")
txtEmpName = userProfileProperties.get_userProfileProperties()['PreferredName'];
document.getElementById('txtEmployeeName').value = txtEmpName;
if (userProfileProperties.get_userProfileProperties()['Title'] != "")
txtTitle = userProfileProperties.get_userProfileProperties()['Title'];
document.getElementById('txtJobTitle').value = txtTitle;
if (userProfileProperties.get_userProfileProperties()['SPS-Department'] != "")
txtDept = userProfileProperties.get_userProfileProperties()['SPS-Department'];
document.getElementById('txtDepartmentName').value = txtDept;
if (userProfileProperties.get_userProfileProperties()['WorkEmail'] != "")
txtEmail = userProfileProperties.get_userProfileProperties()['WorkEmail'];
document.getElementById('txtWorkEmail').value = txtEmail;
if (userProfileProperties.get_userProfileProperties()['UserName'] != "")
txtEmpId = userProfileProperties.get_userProfileProperties()['UserName'];
document.getElementById('txtEmployeeId').value = txtEmpId ;
messageText = "<b>Employee Name:</b> " + txtFirstName + " " +txtLastName;
messageText += "<br/>";
messageText += "<b>Title:</b> " + txtTitle;
messageText += "<br/>";
messageText += "<b>Deparment:</b> " + txtDeparment;
messageText += "<br/>";
messageText += "<b>Employee ID:</b> " + txtEmployeeId;
messageText += "<br/>";
messageText += "<b>Work Email:</b> " + txtWorkEmail;
$get("results").innerHTML = messageText;
}
// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
$get("results").innerHTML = "Error: " + args.get_message();
//window.location.href = "Thankyou.aspx?uname=" + txtTitle ;
}
<!--script src="/_layouts/15/Scripts/MicrosoftAjax.js" type="text/javascript"></script-->
<!--script src="/_layouts/15/init.js" type="text/javascript"></script-->
<script src="/_layouts/15/sp.runtime.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.js" type="text/javascript"></script>
<script src="/_layouts/15/SP.UserProfiles.js" type="text/javascript"></script>
<script type="text/javascript">
//$(document).ready(function(){
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
//});
var userProfileProperties;
function getUserProperties() {
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
userProfileProperties = peopleManager.getMyProperties();
clientContext.load(userProfileProperties);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}
// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
var messageText = "<b>";
var txtEmpName;
var txtTitle;
var txtDept ;
var txtEmail ;
var txtEmpId ;
if (userProfileProperties.get_userProfileProperties()['PreferredName'] != "")
txtEmpName = userProfileProperties.get_userProfileProperties()['PreferredName'];
document.getElementById('txtEmployeeName').value = txtEmpName;
if (userProfileProperties.get_userProfileProperties()['Title'] != "")
txtTitle = userProfileProperties.get_userProfileProperties()['Title'];
document.getElementById('txtJobTitle').value = txtTitle;
if (userProfileProperties.get_userProfileProperties()['SPS-Department'] != "")
txtDept = userProfileProperties.get_userProfileProperties()['SPS-Department'];
document.getElementById('txtDepartmentName').value = txtDept;
if (userProfileProperties.get_userProfileProperties()['WorkEmail'] != "")
txtEmail = userProfileProperties.get_userProfileProperties()['WorkEmail'];
document.getElementById('txtWorkEmail').value = txtEmail;
if (userProfileProperties.get_userProfileProperties()['UserName'] != "")
txtEmpId = userProfileProperties.get_userProfileProperties()['UserName'];
document.getElementById('txtEmployeeId').value = txtEmpId ;
messageText = "<b>Employee Name:</b> " + txtFirstName + " " +txtLastName;
messageText += "<br/>";
messageText += "<b>Title:</b> " + txtTitle;
messageText += "<br/>";
messageText += "<b>Deparment:</b> " + txtDeparment;
messageText += "<br/>";
messageText += "<b>Employee ID:</b> " + txtEmployeeId;
messageText += "<br/>";
messageText += "<b>Work Email:</b> " + txtWorkEmail;
$get("results").innerHTML = messageText;
}
// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
$get("results").innerHTML = "Error: " + args.get_message();
//window.location.href = "Thankyou.aspx?uname=" + txtTitle ;
}