1

I am trying to get the SharePoint 2013 API for GetUserProfileProperty, when I put the below url into a browser it works finr, however when i run the below code, I get no result (I do get a return, but the property is blank - LOG: D1: {"d":{"GetUserProfilePropertyFor":""}} ) (stringify was only used to see the results without needing debug

$.ajax({
url: "https://sp.net/sites/xmas/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='LastName')?@v='MAIN\R5454D'",
type: "GET",
headers: { "accept": "application/json;odata=verbose" },
success: function (d1) {
    var res = JSON.stringify(d1)
    if (console) { console.log('D1: ' + res); };
},
});
2
  • 2
    You need to escape the backslash properly in he username Commented Sep 2, 2015 at 20:03
  • Perfect, thanks Robert - just that simple, replaced \ with %5C (I had tried encoding the whole URL but not just the backslash! Commented Sep 3, 2015 at 6:56

1 Answer 1

0

I'm using the script below to check the users department and set a link on the startpage directly to his/her department-teamsite. I did this in my custom masterpage.

Don't forget to add a link to e.g. "jquery-1.8.3.min.js" in your head tag.

Maybe it helps.

<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 IT Team");
            document.getElementById("teamoverview").innerHTML = "<a href='/teams/"+departmentArrayURL[0]+"'>Workspace</a>";
            break;
        case departmentArray[1]:
            // alert("case HR Team");
            document.getElementById("teamoverview").innerHTML = "<a href='/teams/"+departmentArrayURL[1]+"'>Workspace</a>";
            break;
        }    
}

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

</script>

I'm a starter in writing scripts like that. So please forgive my script skills.

If you like it, give it a vote up, or mark it as the answer if it fits for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.