2

Is it possible to get the department of the current user using REST API (Javascript)?

6

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:

SharePoint 2013: Get UserProfile Properties with REST API

3
  • Thank you @Arsalan... I was killing myself on this, the thing is that it seems that the property is not configured in my server
    – yngrdyn
    May 8 '15 at 14:09
  • 1
    how about current user? without passing @v parameter Nov 10 '16 at 9:49
  • http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties This call will give you all properties for current user. Nov 15 '16 at 5:25
0

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);
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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