1

I am tryin to get some info from PeopleManager.getMyProperties() function. I get the object,some values are null.when i check it from User Profile from Management,I can see the value. How can i fix this one ?

There is my working code to get object.
Note : I want to access Custom Property from User Profile which I created before. I can see the property in the object but value is not coming.

Thank You All..

  $(document).ready(function(){         
    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js'); 
  });

  var userProfileProperties;

  function loadUserData(){

    //Get Current Context   
    var clientContext = new SP.ClientContext.get_current();

    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    //Get properties of the current user
    userProfileProperties = peopleManager.getMyProperties();

    clientContext.load(userProfileProperties);

    //Execute the Query.
    clientContext.executeQueryAsync(onSuccess, onFail);

  }

  function onSuccess() {        
       console.log(userProfileProperties)    
  }

  function onFail(sender, args) {
       console.log("Error: " + args.get_message());
  } 
Cem Berk
  • 11
  • 5

2 Answers2

1

Try the below code and let me know. It works fine for me. I have passed the user name instead of My account. So that you can pass any user account here.

    function getUserProperties(userName) {    
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var profilePropertyNames = ["FirstName", "LastName", "CustomProperty"]; //Have to load all the needed properties here
    var targetUser = userName.trim();
    var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
    userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);    
    clientContext.load(userProfilePropertiesForUser);
    clientContext.executeQueryAsync(onSuccess, onFail);

}

function onSuccess() {    
// userProfileProperties result index is same as the properties loaded above
    var firstName=userProfileProperties[0]; 
    var lastName=userProfileProperties[1];
    var customprop=userProfileProperties[2];
}

Mark it as answer if it helps.

Naveen Prasath
  • 539
  • 1
  • 3
  • 23
  • I tried the code, I got the properties but custom property is still null.Thanks for "username" advice and thanks for help. I will look and when ı find, ı will let you know. @NaveenPrasath – Cem Berk Aug 17 '17 at 06:15
0

I forget to write the solution,sorry for that one.

I tried the code which written by @NaveenPrasath. It is giving a lot of fields but it didn't return "Custom Prop Field".

Working code is shown below.

function getUserProperties(targetUser) {

    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    personProperties = peopleManager.getPropertiesFor(targetUser);
    clientContext.load(personProperties);
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}

function onRequestSuccess() {
        var fullName = personProperties.get_userProfileProperties()['CustomPropField'];
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Cem Berk
  • 11
  • 5