I am did something similar with REST to get the users department.
Then I set a link depending on the department he/she is in, onto my page.
<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" };
$.ajax({
url : requestUri,
contentType : "application/json;odata=verbose",
headers : requestHeaders,
success : onSuccessA,
error : onErrorA
});
}
function onSuccessA(data, request){
// alert("Success One");
// 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) {
alert(error);
}
GetCurrentUser();
});
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" };
$.ajax({
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(
"information-technology-workspace",
"human-resources-workspace");
// set link to logged in users department
switch(department){
default:
// alert("case default");
break;
case departmentArray[0]:
// alert("case Information Technology");
document.getElementById("teamuebersicht").innerHTML = "<a href='/teams/"+departmentArrayURL[0]+"'>Workspace</a>";
break;
case departmentArray[1]:
// alert("case Human Resources");
document.getElementById("teamuebersicht").innerHTML = "<a href='/teams/"+departmentArrayURL[1]+"'>Workspace</a>";
break;
}
}
function onErrorB(jqxr,errorCode,errorThrown){
alert("Error: " + jqxr.responseText)
}
</script>