Is it possible to get the department of the current user using REST API (Javascript)?
2 Answers
Yes, it is possible.. Try the following request:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Department
If this doesn't work you might need to use the following:
For Office 365/SharePoint Online:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Department')?@v='i:0%23.f|membership|user@siteurl.onmicrosoft.com'
For SharePoint 2013 On-Premise:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Department')?@v='domain\username'
Reference:
-
Thank you @Arsalan... I was killing myself on this, the thing is that it seems that the property is not configured in my server– yngrdynMay 8, 2015 at 14:09
-
1
-
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties
This call will give you all properties for current user. Nov 15, 2016 at 5:25
Another way to do this without passing @v parameter:
var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
$.ajax({
url: siteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
for (var i = 0; i < data.d.UserProfileProperties.results.length; i++) {
switch (data.d.UserProfileProperties.results[i].Key) {
case "Department":
alert(data.d.UserProfileProperties.results[i].Value);
break;
}
}
},
error: function (error) {
console.log(error);
}
});