Inserting User Data Into People Picker

May 24, 2012 at 11:38 AM

Hi

I am trying to insert various fields from user data into a NewForm.aspx. Things like Name, department etc are simple as they are single lines of text. My problem occurs when I try to insert a value from the Current User Profile into a people picker. I found Marc's code that seems to help to do this and have inserted alerts for debug purposes to make sure it's getting to where I think it should be getting to ......

var columnName = 'Applicant Email';  

var userName = 'domain\\User'; //obviously I have changedthis to the relevant user ID but I didn't want to put my login credentials into here 

// 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() {  
alert("In Function") //added by Ben
	// Check for the right comment  
	if(searchText.test($(this).html())) {  
	$(this).find("div[Title='People Picker']").html(userName);
		alert("Returning"); //added by Ben
		return false;  
	}  
}); 

 

The alerts show me that this is getting into the function and after a few iterations the "Returning" alert shows as well. However, nothing is being populated into the People Picker - am I doing something really silly again ? (New to all this Javascript business)

Coordinator
May 24, 2012 at 12:02 PM

Ben:

I'm working on a new function for SPServices to help with this. Want to try it out? Here's a blog post, and here's the more up to date code.

M.

// Find a People Picker in the page
// Returns references to:
//   row - The TR which contains the People Picker (useful if you'd like to hide it at some point)
//   contents - The element which contains the current value
//   currentValue - The current value if it is set
//   checkNames - The Check Names image (in case you'd like to click it at some point)$.fn.findPeoplePicker = function(options) {
$.fn.findPeoplePicker = function(options) {

  var opt = $.extend({}, {
    peoplePickerDisplayName: "",	// The displayName of the People Picker on the form
    valueToSet: "",			// The value to set the People Picker to. Should be a string containing each username or groupname separated by semi-colons.
    checkNames: true			// If set to true, the Check Names image will be clicked to resolve the names
  }, options);


	var opt = $.extend({}, {
		peoplePickerDisplayName: "",	// The displayName of the People Picker on the form
		valueToSet: "",					// The value to set the People Picker to. Should be a string containing each username or groupname separated by semi-colons.
		checkNames: true				// If set to true, the Check Names image will be clicked to resolve the names
	}, options);

	var thisRow = $("nobr").filter(function() {
		// Ensures we get a match whether or not the People Picker is required (if required, the nobr contains a span also)
		return $(this).contents().eq(0).text() == opt.peoplePickerDisplayName;	
	}).closest("tr");
	var thisContents = thisRow.find("div[Title='People Picker']");
	var thisCheckNames = thisRow.find("img[Title='Check Names']:first");
	if(opt.valueToSet.length > 0) thisContents.html(opt.valueToSet);
	if(opt.checkNames) thisCheckNames.click();
	var thisCurrentValue = thisContents.text().trim();

	// Parse the entity data
	var dictionaryEntries = [];
	thisRow.find("div[title='People Picker'] > span").each(function() {
		
		var thisData = $(this).find("div:eq(1)").attr("data");
		var arrayOfDictionaryEntry = $.parseXML(thisData);
		$xml = $(arrayOfDictionaryEntry);

		var dictionaryEntry = {};
		$xml.find("DictionaryEntry").each(function() {
			var key = $(this).find("Key").text();
			var value = $(this).find("Value").text();
			dictionaryEntry[key] = value;
		});
		dictionaryEntries.push(dictionaryEntry);
	
	});
	
	return {row: thisRow, contents: thisContents, currentValue: thisCurrentValue, checkNames: thisCheckNames, dictionaryEntries: dictionaryEntries};
}

May 24, 2012 at 12:14 PM

I shall try this out right now !   Will get back to you as soon as I can to tell you how I go.

Thanks yet again

Ben

May 24, 2012 at 12:43 PM

Hmmm - a bit stuck I'm afraid

I put in the function at the end of my script as you pasted it above. Then I tried calling the function with the following call (modified from the blog that you linked) :

// Set the Site Contact to the current user  
appEmailPeoplePicker = $().findPeoplePicker({  
	peoplePickerDisplayName: "Applicant Email",
	valueToSet: $().SPServices.SPGetCurrentUser() 
	});  

I got an "Object doesn't support this property or method" error when I looked in the IE debugger - it pointed straight at the "appEmailPeoplePicker = $().findPeoplePicker({" line. I deleted the ValueToSet line and the preceding comma to try to simply return the peoplepicker object without setting the value and the same error happened.

I am obviously doing something silly but cannot spot it

May 24, 2012 at 1:31 PM

Yep - was doing something silly. I called the function before the "$(document).ready(function() {" line in my code - doh !!!

So now I can get things into the People Picker - all I need to do now is make it the email address rather than the user itself.

Thanks again Marc

May 24, 2012 at 1:45 PM

And now - all sorted - list is being updated with all sorts of different user data now :)

Cheers,

Ben