0

I am needing to get the value of what was entered in people picker field, and automatically populate this value into a second people picker field on save. (on my list I need to display both the persons name and their phone in separate columns).

Most of the examples I have found so far are can be used to successfully populate multiple people picker fields with the current ID of the person that is logged on, but obviously are not what I need as the ID does not necessarily need to be the same as the person currently logged on.

(Examples are here: https://social.msdn.microsoft.com/Forums/office/en-US/872785c7-4179-488e-9239-1a9c81d48eda/set-the-column-value-to-current-userid-if-check-box-is-checked?forum=sharepointdevelopmentprevious)

I have cobbled the following Web Part script together of all the different techniques I have discovered so far, using a column name of Uid, but none is working.

<html>
<script src="https://oursites.../jquery-1.11.3.js"></script><script>
// <![CDATA[
$(document).ready(function () {
alert('Executing OK');
});
function PreSaveAction(){
   (window.jQuery || document.write('<script src="https://oursites..../jquery.SPServices-0.7.2.min.js"><\/script>'));
   (window.jQuery || document.write('<script src="https://oursites.../jquery-1.11.3.js"><\/script>'));

   alert('in save');

  var ppDiv = $("[id$='ClientPeoplePicker'][title='Uid']");
  alert(ppDiv);
  var ppEditor = ppDiv.find("[title='Uid']");
  alert(ppEditor);
  var spPP = SPClientPeoplePicker.SPClientPeoplePickerDict[ppDiv[0].id];
  alert(spPP);
  var ppEditorval =  ppEditor.val();

  alert(ppEditorval);

  var fieldName = "Uid";
  var _PeoplePicker = $("div[title='" + fieldName + "']");
  alert(_PeoplePicker);
  var _PeoplePickerTopId = _PeoplePicker.attr('id');
  alert(_PeoplePickerTopId);
  var _PeoplePickerEditer = $("input[title='" + fieldName + "']");
  alert(_PeoplePickerEditer);
  var ppEdit = _PeoplePickerEditer.val();
  alert(ppEdit);

  var PickerTitle = "Uid";
  var PickerDiv = $('[id$="ClientPeoplePicker"][title="' + PickerTitle + '"]');
  alert(PickerDiv);
  var PickerEditor = PickerDiv.find('[title="' + PickerTitle + '"]');
  alert(PickerEditor);
  var PickerInstance = SPClientPeoplePicker.SPClientPeoplePickerDict[PickerDiv[0].id];
  alert(PickerInstance);
  var ppchk =   $('td nobr:contains("' + PickerTitle + '")').eq(0).closest('tr').next('td').text();
  alert(ppchk);
  alert('stop');
  return false;
};
// ]]></script>
</html>

The following are the values from the Trace Alerts:
- All fields have a value of object Object] except for the following fields:
- ppEditorval, ppEdit and ppchk which are all blanks
- _PeoplePickerTopId which has a value of Uid......$ClientPeoplePicker

Additionally I also tried the following code as well

var salesRep = $().SPFindPeoplePicker({
    peoplePickerDisplayName: "Uid"
}); 
var sr = salesRep.currentValue;
alert(sr);
var sruid = salesRep.SPUserid;
var sruidtitle = salesRep.row.find("span[class='ms-entity-resolved']").attr("title");
var pp = $().SPFindPeoplePicker({ peoplePickerDisplayName: "Uid" });
alert(pp);

But none of this code worked - I received the error message

SCRIPT438: Object doesn't support property or method 'SPFindPeoplePicker'

Help! How do I get the value of a People Picker field?

1

Ah, after sorting through so many posts on this topic, all of which just give some clues, I finally managed to work it out courtesy of the PF12 debugger and trying to put the jigsaw puzzle together.

Here is the scenario: I wish to record a userid and their office two separate fields in my list. In defining the columns, I specify that I want different show fields from the people picker, one field Uid to have the name, another, Uid2 to be the Office. Uid2 will be hidden from the newform. The value of Uid will be copied into Uid2, which will be done on save of the Newform.

(Note I've got the return false in this code for checking purposes)

I suspect some pruning of the document.write statements can be done in the following code.

References are:

https://jasonscript.wordpress.com/2013/08/07/javascript-and-working-with-the-sharepoint-2013-people-picker/

http://www.getsharepointsolutions.com/sharepoint-2013-pre-populate-people-picker-value-to-current-user/

Here's the code: Enjoy.

<html>
<script src="https://oursites..../jquery-1.11.3.js"></script><script>
// <![CDATA[
$(document).ready(function () {
alert('Executing OK');
});

function PreSaveAction(){
 (window.jQuery || document.write('<script src="https://oursites..../jquery.SPServices-0.7.2.min.js"><\/script>'));
 (window.jQuery || document.write('<script src="https://oursites..../jquery-1.11.3.js"><\/script>'));

  function SetUserFieldValue(fieldName, userName) {

    var _PeoplePicker = $("div[title='" + fieldName + "']");
    var _PeoplePickerTopId = _PeoplePicker.attr('id');
    var _PeoplePickerEditer = $("input[title='" + fieldName + "']");
    _PeoplePickerEditer.val(userName);
    var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
    _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
  }

   var controlName  = "Uid";                                                    // My People Picker Control is called "Uid"
   var ppDiv        = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");         // Select the People Picker DIV
   var ppEditor     = ppDiv.find("[title='" + controlName + "']");                                  // Use the PP DIV to narrow the jQuery scope
   var spPP         = SPClientPeoplePicker.SPClientPeoplePickerDict[ppDiv[0].id];   
   var users = spPP.GetAllUserInfo();
   for (var i = 0; i < users.length; i++) {
     var user = users[i];
     var Uiduser = user['Key'].split("i:0#.w|")[1]; 
    };

   SetUserFieldValue("Uid2",Uiduser); 
   return false;
   };
   // ]]></script>
 </html>

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.