Get userproperty to change link in masterpage (en)

Get userproperty to change link in masterpage en Title
, ,

In this short blog post I want to show you how to get a specific user property of the “current loged in user” and due to that, change a link on the masterpage.

I am going to change the text “teamoverview” into “workspace” and set the link from “/teams” to “/teams/<teamsite>”

Why I am doing this? Because I want to give the user the quickest way to access their teamsite from the startpage.

Masterpage before:

Get userproperty to change link in masterpage (en)_1


Masterpage after:

Get userproperty to change link in masterpage (en)_2


Lets go!

Open up SharePoint Designer and check out your Masterpage. Please copy the masterpage so in case you got a backup.

Edit the masterpage in advanced view and between the PlaceHolderMain:

Get userproperty to change link in masterpage (en)_3


paste the following script:

<script type="text/javascript">

$(document).ready(function() {
var loginName = "";
var userid = _spPageContextInfo.userId;

function GetCurrentUser() {

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";

var requestHeaders = { "accept" : "application/json;odata=verbose" };

 url : requestUri,
 contentType : "application/json;odata=verbose",
 headers : requestHeaders,
 success : onSuccessA,
 error : onErrorA

function onSuccessA(data, request){
 // alert("Success A");
 // cutting of i:0#.w|
 var loginName = data.d.LoginName.split('|')[1];

 var repOne = "%5C"; 
 var repTwo = "%2E";
 // alert("before replace: " + loginName); 
 loginName = loginName.replace(/\/g, repOne); // replace  with %5C
 loginName = loginName.replace(/./g, repTwo); // replace . with %2E
 // alert("after replace: " + loginName);
 GetCurrentUserDepartment(loginName); // call next function to get department. Pass the loginname into the function

function onErrorA(error) {




function GetCurrentUserDepartment(loginNameAcc) {

 // alert("LoginName: " + loginNameAcc)
 var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Department')?@v=" + "'" + loginNameAcc + "'";

 var requestHeaders = { "accept" : "application/json;odata=verbose" };

 url : requestUri,
 method: "GET",
 headers : requestHeaders,
 success : onSuccessB,
 error : onErrorB

function onSuccessB(data){
 var department = data.d.GetUserProfilePropertyFor;
 // alert("Team:" + department);
 var departmentArray = new Array( 
 "Information Technology",
 "Human Resources");
 var departmentArrayURL = new Array( 

 // set link to logged in users department
 // alert("case default");
 case departmentArray[0]:
 // alert("case IT Team");
 document.getElementById("teamoverview").innerHTML = "<a href='/teams/"+departmentArrayURL[0]+"'>Workspace</a>";
 case departmentArray[1]:
 // alert("case HR Team");
 document.getElementById("teamoverview").innerHTML = "<a href='/teams/"+departmentArrayURL[1]+"'>Workspace</a>";

function onErrorB(jqxr,errorCode,errorThrown){
 alert("Error: " + jqxr.responseText)


I tried to comment some parts of the script, to give you some information what it is doing.

That’s it. Hopefully you have now a better idea how to “get userproperty change link in masterpage”.

If you have questions, don’t be afraIT to comment and if you like it, share it 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *