Monday, March 20, 2017

Get User Properties using User Profile in SharePoint Online

Below are some important properties which we can retrieve from user profile for a user by passing login id.

  • Cell Phone Number
  • Manager
  • Accountname
  • Displayname
  • Directreports
  • Email
  • Extendedmanagers
  • FirstName
  • LastName
  • Department
  • PictureURL
  • UserName etc..


I would be retrieving it using Login Id of the desired user. Login id by default in SharePoint Online is of below format.

i:0#.f|membership|manjot@xyz.com

If we directly pass this format it will give you “bad request” error since while passing URL to rest calls we need to encode few characters. In above case, we need to encode ‘#’ to ‘%23’. So our login id would be:

i:0%23.f|membership|manjot@xyz.com


Now pass login name as in below code which will give you the required property

var loginName='i:0%23.f|membership|manjot@xyz.com';

GetSPListItems("/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='CellPhone')?@v=" + "'" + loginName + "'", function(data) {
  
    console.log(data.d.GetUserProfilePropertyFor); // This will give you cell phone number

}, function(error) {
    console.log(error);

});

function GetSPListItems(url, Success, Error) {
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-Type": "application/json;odata=verbose"
        },
        success: Success,
        error: Error
    });
}

If you want to access all properties then simply replace the URL in the above code with below value.
  // in above url we specified propertyName='CellPhone' to get specific property if you want to access all then simply remove that propertyName parameter.

 URL= _api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v)?@v=" + "'" + loginName + "'  

No comments:

Post a Comment