如何使用带有rest api的JSOM检索自定义用户配置文件属性。
因为我需要检索当前登录用户的员工成本中心(自定义属性) 但是下面的代码不起作用。
<script>
$ .ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties",
headers: { Accept: "application/json;odata=verbose" },
success: function (data) {
try {
// userDisplayName=data.d.DisplayName;
//AccountName = data.d.AccountName;
var properties = data.d.UserProfileProperties.results;
for (var i = 0; i < properties.length; i++) {
if (properties[i].Key == "Employee Cost Center") {
var CostCenter= properties[i].Value;
}
}
$('#Diviision').text(CostCenter);
} catch (err2) {
}
},
error: function (jQxhr, errorCode, errorThrown) {
alert(errorThrown);
}
});
</script>
答案 0 :(得分:1)
修改代码如下:
<div id="Diviision"></div>
<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function () {
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
//execute AJAX request
$.ajax({
url: requestUri,
type: "GET",
headers: { "ACCEPT": "application/json;odata=verbose" },
async: false,
success: function (data) {
var properties = data.d.UserProfileProperties.results;
for (var i = 0; i < properties.length; i++) {
if (properties[i].Key == "Employee Cost Center") {
var CostCenter= properties[i].Value;
$('#Diviision').text(CostCenter);
}
}
},
error: function () {
//alert("Failed to get details");
}
});
});
</script>