Skip to main content
add code
Source Link
Asad Refai
  • 6k
  • 8
  • 35
  • 57

If you want to get user profile properties for a current user via REST API, then you can either pass user id and get all properties including your own custom or use method GetMyProperties.

Code where you pass userId.

userInfo.UserProfileProperties.results[10] is a bit of code where you get available user profile properties. If I am not wrong then custom properties will be at the last indexes like 60, 61 this you need to identify.

function getUserInfoSuccess (data) {
    var userInfo = data.d;
    alert(userInfo.UserProfileProperties.results[10]);     
}

function getUserName() {
    var clientContext = SP.ClientContext.get_current(),
        userAccount = clientContext.get_web().getUserById(_spPageContextInfo.userId);

    clientContext.load(userAccount); 
    clientContext.executeQueryAsync(function () {

        var index = userAccount.get_loginName().indexOf('|') + 1,
            loginName = userAccount.get_loginName().substring(index);

        jQuery.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/getpropertiesfor(@v)?@v='" + loginName + "'",
            type: "GET",
            headers: {
                "Accept": "application/json;odata=verbose"
            },
            success: function (data) {
                getUserInfoSuccess(data);
            },
            error: function () {
                alert("Failed to get user. Please refresh page.");
            }
        });
    },
        function () {
            alert("Failed to get user. Please refresh page.");
        });
}

Usage of GetMyProperties

If you want to get user profile properties for a current user via REST API, then you can use method GetMyProperties.

If you want to get user profile properties for a current user via REST API, then you can either pass user id and get all properties including your own custom or use method GetMyProperties.

Code where you pass userId.

userInfo.UserProfileProperties.results[10] is a bit of code where you get available user profile properties. If I am not wrong then custom properties will be at the last indexes like 60, 61 this you need to identify.

function getUserInfoSuccess (data) {
    var userInfo = data.d;
    alert(userInfo.UserProfileProperties.results[10]);     
}

function getUserName() {
    var clientContext = SP.ClientContext.get_current(),
        userAccount = clientContext.get_web().getUserById(_spPageContextInfo.userId);

    clientContext.load(userAccount); 
    clientContext.executeQueryAsync(function () {

        var index = userAccount.get_loginName().indexOf('|') + 1,
            loginName = userAccount.get_loginName().substring(index);

        jQuery.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/getpropertiesfor(@v)?@v='" + loginName + "'",
            type: "GET",
            headers: {
                "Accept": "application/json;odata=verbose"
            },
            success: function (data) {
                getUserInfoSuccess(data);
            },
            error: function () {
                alert("Failed to get user. Please refresh page.");
            }
        });
    },
        function () {
            alert("Failed to get user. Please refresh page.");
        });
}

Usage of GetMyProperties

Source Link
Asad Refai
  • 6k
  • 8
  • 35
  • 57

If you want to get user profile properties for a current user via REST API, then you can use method GetMyProperties.

  1. Get all properties of current user:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties

  2. Get single property of current user:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties/PictureUrl OR

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl

  3. Get Multiple Properties for the current user:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl,AccountName

For more details refer SharePoint 2013: Get UserProfile Properties with REST API