Set a People Picker’s Value on a Form

Following up on my recent post about getting the current value of a People Picker on a form, today I built the JavaScript to set the value, and here it is. As usual, I’ve left in my debugging code in case you want to see how it works.

// setPickerInputElement: Set a People Picker's value using its identifier to find it in the page
// Arguments:
//                            identifier:            The identifier for the instance of the fieldName (ff1, ff2, etc.)
//                            value:                    Set the fieldName to this value
//

function setPickerInputElement(identifier, value) {
  var tags = document.getElementsByTagName('DIV');

  for (var i=0; i < tags.length; i++) {
    var tempString = tags&#91;i&#93;.id;
    //alert('tags&#91;' + i + '&#93;.id = ' + tempString);
    if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf('UserField_upLevelDiv') > 0)){
      //alert('HIT for ' + identifier + ' id=' + tags[i].id + ' value=' + tags[i].value);
      tags[i].innerHTML = value;
      break;
    }
  }
}

Similar Posts

19 Comments

  1. Thank you so much, for the information, I had not find nothing to retrieve the values in a people picker. thanks

  2. THANK YOU, Marc!

    Your information is so helpful. I great appreciate your advice and knowledge.

    Sincerely,

    Xixi

        1. Aaron:

          This is just a standalone function. You’ll need to use it in conjunction with the other post I pointed you to to set your People Picker value(s). Note that the People Picker takes a person’s name, not their account, generally speaking; it depends on how you have your users set up. You should use whatever type of value you would normally type into the People Picker.

          M.

  3. I have a requirement wherein i need to set the choices for a Multilookup field on a Sharepoint form where in the values will be coming from another list.

    The choices will be depending on the value of the Start Date and End Date fields. My initial idea was to use a jQuery script to send a CAML Query to the source list retrieve the results (in this requirement the user names) and put them as the lookup choices. Is this feasible?

    1. Jojo:

      I’d use a Lookup column which simply pulls all of the values (the default SharePoint behavior). Then in the script on the page, build the logic you need with jQuery and GetListItems. Take a look at how SPCascadeDropdowns in SPServices works for ideas.

      M.

  4. You can do it quick with jquery (tested in SP2010)

    $(“nobr:contains(‘FieldNameHere’)”).parents(‘div:first’).find(“[id*=’UserField_upLevelDiv’]”).html(ValueHere); // Sets the login

  5. Marc,

    Noob question: You often use this argument in People Picker functions “// Find a People Picker’s value using its identifier (ff1, ff2, etc.)to find it in the page ”

    I don’t quite understand how to find a the proper identifier value — though I can usually find the first People Picker on a page — What does the “ff1, ff2” indicate? Is the argument a number, text, or what?

    In advance, thanks, and sorry for the noob question!

    1. Ben:

      When you create a custom form, SharePoint adds prefixes to the id of each element in the form, so they look something like:
      ctl00_m_g_794be4b6_2104_459e_8e83_2615a398f711_ff1_1_ctl00_ctl00_TextField
      In OOB forms, the ids look something like this:
      ctl00_m_g_0fc22f7a_6c17_4981_bbd9_f5c12e27bccd_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField
      Regardless, you need some way to find the element in the DOM. In my examples, I’m searching for some part of the id, often ffn, where n=1,2,3, etc.

      M.

Leave a Reply to Ben Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.