Get value from People Picker and set into singleline textbox

Hello,
Following is my UI.

I want to get the person from the I would like to compliment field and find out the person's department.
After find out the department, I want to set into Dept field.

Following is the code I try to use:

$(document).ready(function(){  
var scriptbase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
   $.getScript(scriptbase + "SP.UserProfiles.js", getUserProperties());
});
});
function getUserProperties() {
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    personProperties = peopleManager.getMyProperties();
    clientContext.load(personProperties);
    clientContext.executeQueryAsync(
                             function () {
                //Set Department
                fd.field('Dept').control().value(personProperties.get_userProfileProperties()['Department']);
            
              });
    }

Currently, my main problem is i could not get the value from the "I would like to compliment" field by using following code:

fd.field('I would like to compliment').value;

Please help. Thank you.

Dear @Tan_Vickey,
When using JavaScript API, it’s important to use the actual InternalName of the fields, and not their display titles. You can find an actual InternalName of the field in the designer, if you select it:
image
Simply replace ‘I would like to compliment’ with the InternalName and it should get the value correctly.

1 Like

Can someone explain to me how this process works. If I have a name/email field with internal name as TeamName and I have a department field with and internal name as TeamDepartment. What is the javascript code so that when I enter the name/email address it pulls the department automatically from Office 365 user profiles

Dear @hutchisoni,
As I’ve replied in the email before, what you need to do is use the following script - https://sharepoint.stackexchange.com/questions/206701/how-to-retrieve-users-profiles-using-javascript-object-model-jsom-in-sharepoin

If you also need to do it on input, you’ll need to detect change in the TeamName, and then send a request for this specific user, instead of
var personName = peopleManager.getUserProfilePropertyFor(user.get_loginName(),'AccountName');

How to detect change in SharePoint fields and how to populate them, you can check here - https://plumsail.com/docs/forms/javascript/fields-sp.html

Dear users,
If you’ll try to implement this functionality in future versions, you might find that JSOM methods are no longer available in Forms for SharePoint. They’ll still work for older forms which were saved with an older version, but you might not be able to achieve it with the newest version, at least not with the JSOM code.

We advise to use pnp-js library instead which is now available in JS editor. For example, to retrieve ALL user properties of the current user, you can use the following code:

pnp.sp.profiles.myProperties.get().then(function(result) {
   var props = result.UserProfileProperties;
   console.log(props);
});
2 Likes

Hi Nikita,

Possibly this is to do with this post but we’ve noticed that until recently we were using this to get user profile object of a people picker field:

fd.field(‘PeoplePickerSPField’).value.EntityData.SPUserId

However we’ve noticed that this is no longer available. It originally returned an object that contained the UserID (integer) for the selected user, however this no longer exists.

We’ve tried fd.field(‘PeoplePickerSPField’).value.EntityData.ObjectId and fd.field(‘PeoplePickerSPField’).value.Key and neither return any values.

Can you suggest how we might do this?

EDIT - looks like fd.field(‘PeoplePickerSPField’).value.EntityData.SPUserId works if the item has been saved, but is not available after field population - could this be a bug?

Dear Andy,

Please use the SharePoint REST API to access the SPUserID: https://www.codeproject.com/Tips/798407/SharePoint-REST-API-Get-User-Id-by-User-Name