3

url: appweburl + "_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Manager')?@v='" + seluser + "'",

For my SP 2013 app I need to get a users manager and I'm using the following code to achieve this:

function getusersManager(selUser) {
//clear any errors
var errModalArea = document.getElementById("modalAnnouncements");
// Remove all nodes from the errAllEvents <DIV> so we have a clean space to write to
while (errModalArea.hasChildNodes()) {
    errModalArea.removeChild(errModalArea.lastChild);
}
var seluser = selUser;
var executor = new SP.RequestExecutor(appweburl.toLowerCase());
executor.executeAsync({
   url: appweburl + "_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Manager')?@v='" + seluser + "'",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: successGetUserManager,
    error: errorGetEmployee

});
}

function successGetUserManager(data) {
var jsonObject = JSON.parse(data.body);
var announcementsHTML = "";
document.getElementById("renderAnnouncements").innerHTML = "<b> Hello " + jsonObject.d.Manager;

//$('#txtManger').val(jsonObject.d.Manager);
}

However this keeps returning a "undefined". Putting the call directly into the browser using

https://xxx.sharepoint.com/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Manager')?@v=%27i:0%23.f|membership|[email protected]%27

Gives me back the correct data but I'm unsure how to get that value...I see no key etc?

<d:GetUserProfilePropertyFor xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">i:0#.f|membership|[email protected]</d:GetUserProfilePropertyFor>

4 Answers 4

3

Try using this url:

appweburl + "/_api/SP.AppContextSite(@target)/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Manager')?@v='" + seluser + "'&@target='" + encodeURIComponent(hostweburl) + "'"

This link has more on the cross-domain library, SP.RequestExecutor.js.

0

Found a solution in my environment.

My script:

$(document).ready(function () {

var loginName = "domain\username";


function GetCurrentUser() {

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Office')?@v=" + "'" + loginName + "'";

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


$.ajax({

  url : requestUri,

  method: "GET",

  headers : requestHeaders,

  success : onSuccess,

  error : onError

});

}


function onSuccess(data){


  var department = data.d.GetUserProfilePropertyFor;

  alert(department);


} 

function onError(jqxr,errorCode,errorThrown){

alert("Error: " + jqxr.responseText)

}

GetCurrentUser();  

});

I think the important part is:

var department = data.d.GetUserProfilePropertyFor;
alert(department);

And my variable loginname with %5C (\) and %2E (.)
encodeURIComponent() doesn't help me here because of (\) and (.)

domain%5Clastname%2Efirstname

You can check out my whole script with "get the current User" and pass the loginname to the next function here: http://1drv.ms/1enskYR where I finally change a link on the page to the users department.

Hopefully my english isn't that bad...

0
**<script src="../SiteAssets/JQUERY/jquery-1.12.3.min.js"></script> 
<script>

var RESTURL = "https://siteurl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='i%3A0%23%2Ew%7Cdomain%5Cloginname'";
$(document).ready(function () {

    GetUserInformation();

});

function GetUserInformation()

{

 var self = this;

 $.ajax({

  url: RESTURL,

  type: "GET",

  headers: {

   "accept": "application/json;odata=verbose",

  },

  success: function (userdata) {

    var department= userdata.d.UserProfileProperties.results[11].Value;
    var manager=userdata.d.UserProfileProperties.results[15].Value;
     alert(Department+"  "+manager);
     alert(userdata.d.Title);

  },

  error: function (error) {

   alert(JSON.stringify(error));

  }

 });



}

 </script> 
    </script>**
0

I had the same issue and it was having my username in this format that solved my issue:

domain%5Clastname%2Efirstname

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.