Get and Set People Picker Value


Friends, this is simple example to save the people picker value in share point list and get the people picker value form list shown to people picker control on the custom form . But most of the developer do very silly mistake and forget the one important scenario while start the coding.

Most of the time issue arise,
If Active Directory having multiple user with same first and last name.

Get value from People Picker Control
Note: - people picker control pass as parameter.
public string GetPeoplePickerValue(PeopleEditor peoplePicker)
        {
            string userValue = string.Empty;
            try
            {
                if (peoplePicker.ResolvedEntities.Count.Equals(1))
                {
                    PickerEntity pickerAppEntity = (PickerEntity)peoplePicker.ResolvedEntities[0];
                    userValue = pickerAppEntity.Key;
                }
            }
            catch (Exception)
            {
                throw;
            }

            return userValue;
        }

Set value to People picker control
Note:- People Picker Control pass as parameter and value as SPListItem Object , where Web object can also pass as parameter.
  public void SetPeoplePickerValue(PeopleEditor peoplePicker, object value)
        {
            try
            {
                if (value != null)
                {
                    SPWeb edgeWeb = SPContext.Current.Web;
                    string user = Convert.ToString(value);
                    SPFieldUserValueCollection userCol = new SPFieldUserValueCollection(edgeWeb, user);
                    peoplePicker.CommaSeparatedAccounts = Convert.ToString(userCol[0].User);
                    peoplePicker.Validate();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

No comments:

Post a Comment