Set a People Picker’s Value on a Form – Revisited with jQuery

Jim Bob Howard (@jbhoward) always asks me good questions.  Today he was interested in setting the value of a People Picker on a form with script.  I pointed him at my old post about doing it with JavaScript, but we’re jQuery buddies, so I wanted to give him some jQuery instead. That and the fact that jQuery will be more reliable.  (Jim Bob was already using jQuery on the page. If he wasn’t I would have suggested sticking with the JavaScript approach; no need for the overhead if JavaScript will do.)

Here’s the jQuery I came up with:

var columnName = 'yourcolumnname';
var userName = 'domain\\username';

// There's no easy way to find one of these columns; we'll look for the comment with the columnName
var searchText = RegExp("FieldName=\"" + columnName + "\"", "gi");
// Loop through all of the ms-formbody table cells
$("td.ms-formbody").each(function() {
    // Check for the right comment
    if(searchText.test($(this).html())) {
        $(this).find("div[Title='People Picker']").html(userName);
        return false;
    }
});

Similar Posts

44 Comments

  1. Very interesting! I downloaded and looked at the source. Then switched my reference on my SPD page. Added the block below to see if my syntax was correct, but now am getting ‘Object doesn’t support this property or method’ in IE8. The column name in one of my people picker columns in Manager. BTW…my jQuery and SPServices libraries are all loaded properly…I go thru the tests with alerts each time :-)

    $(document).ready(function(){
    var pp = $().SPServices.SPFindPeoplePicker(
    {
    peoplePickerDisplayName: ‘Manager’,
    valueToSet: ‘mydomain\\myaccount’,
    checkNames: false
    });
    });

    Thanks for your time sir!

    1. Jason:

      It’s hard to say. If you debug a bit in the browser, you should be able to determine where the error is happening. Based on the text of it, I would think it would be one of the .js file references, but it sounds like you’ve checked that.

      M.

      1. I have no idea why it does not work. Other SPServices functions are working, but this particular functions throws an error in IE8. I saw this posting on EUS which is the heart of what I’m trying to accomplish as well:

        http://www.endusersharepoint.com/STP/viewtopic.php?f=13&t=2898

        Someone posted something that supposedly works, but when I try it requires two clicks of the check names button. That won’t work in a production app. Any guidance is appreciated!

          1. That’s the one I’m using now. I switched it based on your comment yesterday. I opened the source and saw the code, so I know it’s there. Is there something wrong with my implementation of it? See above at 8:08am. Regardless, my objective is to just set the manager people picker at run time when the employee people picker value is set…passing in the value of the textbox into a function that will set the manager field automatically. I’m starting to think this is unchartered territory ;-)

            1. Jason:

              Let’s move this discussion over to the SPServices Discussions. It’s a little easier to post and format code there. I’d like to get this working for you. It’ll also help me to identify whether there’s a problem with this new function.

              M.

  2. Hi Guys,
    I am trying to do the same thing with Nintex Forms
    In my initiation form I have few people picker fields and I want to get the filed values and use for validation. I was not able to find anything online so any thoughts or sample code that you guys provide?

Leave a 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.