This is what the pre-parsed data looks like in the console after querying
http://mysharepointsite/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Department
:
I am trying to get the department of the current user and I am having a lot of difficulty.
I am currently at a stage where I am trying to copy this SE question, and I managed to at least get somewhere by running this in PowerShell:
$response = Invoke-WebRequest http://mysharepointsite/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Department -UseDefaultCredentials
$response.Content
Where Content
does in fact contain my department (IT), but it is hidden in a load of xml data which I don't need.
I have no idea how to get just the department name using javascript, but this is the code I have at the moment (which is being executed from a content editor web part):
function getUser() {
var userid = _spPageContextInfo.userId;
var requestUri = "http://mysharepointsite/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
var requestHeaders = { "accept": "application/json;odata=verbose" };
$.ajax({
url: requestUri,
contentType: "application/json;odata=verbose",
headers: requestHeaders,
success: onSuccess,
error: onError
});
function onSuccess(data, request) {
var x = data.department;
alert(x);
}
function onError(error) {
alert("Error on retrieving current user.");
}
}
$(document).ready(function() {
getUser();
});
Where in this case, I just get an alert which says undefined
. Can anyone please help me understand how to parse this incoming data?
Invalid character
error when this line runs :var parsedData = JSON.parse(data);
– Bassie Jun 2 '16 at 15:45console.log(data);
looks like in the console... it is pretty confusing - I have no idea what to do with that! – Bassie Jun 2 '16 at 15:56