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[i].id; //alert('tags[' + i + '].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; } } }
Thank you so much, for the information, I had not find nothing to retrieve the values in a people picker. thanks
THANK YOU, Marc!
Your information is so helpful. I great appreciate your advice and knowledge.
Sincerely,
Xixi
I’m not sure if you check this very often or not, but once you do insert this code into your page, how is the value of the people picker set? Can you set it through the url? Example–> http://blah/NewForm.aspx?Assigned%20to=SP\acb. How does it work? Thanks.
Aaron:
You can use this function to set a People Picker’s value based on a Query String parameter. Check out this old post http://sympmarc.com/2008/01/31/pre-populating-sharepoint-list-item-values-using-query-string-variables for info on how you can make that happen in general. You would use this function rather than the others for a People Picker.
Hope this helps…
M.
I put this script in my NewForm.aspx page and tried to use the query to fill my people picker field, Secondary Assignee. These are all of the queries that i tried.
http://blah?Secondary Assignee=DOMAIN\username
http://blah?Secondary%20Assignee=DOMAIN\username
http://blah?Secondary Assignee=username
http://blah?Secondary%20Assignee=username
Is there something i am doing wrong?
do i need to change any values or can i just strictly copy this code?
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.
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?
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.
Thank you for this. It works fine in SP 2010 as well. Before finding this I was really confused looking for a people picker textbox in the source code.
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
How to swap multiple people login details from one people picker control to another?
ikum:
Well, you’ll need to have your script grab all of the values from the first people picker and populate the other.
Note that I also have a post which shows how to grab the value with jQuery:
http://sympmarc.com/2010/03/16/set-a-people-pickers-value-on-a-form-revisited-with-jquery/
M.
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!
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.