In one SharePoint 2013 portal, we had a requirement to
change display name of current logged in User, which appears in Welcome Menu.
We had below script in master page, which successfully changed
display name as First Name & Last Name.
_ spPageContextInfo.userId will give user id in User Info List.
After
that we can construct REST URL to access User Info List and retrieve User Details
(First Name, Last Name, Department, Email etc.) using REST API.
.ms-core-menu-root CSS
class gives User Display Name object and by using Jquery syntax below we can
set name as per our requirement.
var userid =
_spPageContextInfo.userId;
var
reqUrl = _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/SiteUserInfoList/Items("+ userid
+")?$select=FirstName,LastName";
var
displayName ;
$.ajax({
url: reqUrl,
type: "GET",
headers: {
"accept":
"application/json;odata=verbose",
},
success: function (data) {
$(".ms-core-menu-root")[1].innerText= data.d.FirstName +
" " + data.d.LastName;
},
error: function (error) {
alert("error in REST API call");
}
});
No comments:
Post a Comment