Friday, April 30, 2010

Get the value of PeoplePicker during postback

Using the People Picker Field in a custom listform is basically straight forward. But I ran into a little problem when I needed to extend the validation of the form based on the user specified in the field:

ResolvedEntities[] vs. Entities[]

I needed to run a CAML Query and use the username as a parameter but the property Entities did not contain any objects during postback.

Solution use the ResolvedEntities instead, this contains the users that have been validated and is persistent during postback…

if (Pe_EffectiveUser.ResolvedEntities.Count != 0)
{

PickerEntity PEffectiveUser = (PickerEntity)Pe_EffectiveUser.ResolvedEntities[0];
//PickerEntity PEffectiveUser = (PickerEntity)Pe_EffectiveUser.Entities[0];
this.EffectiveUser = PEffectiveUser.Description;

String UserID = this.EffectiveUser.Substring(this.EffectiveUser.IndexOf("\\") + 1);
}
else
{
// Show a button to validate before continuing
}