How to clear ClientPeoplePicker field from a button click event handler?
-
08-10-2020 - |
Question
I am building a custom form that includes a ClientPeoplePicker control.
When the user press the Clear button, all the fields in the forms should be reset to empty/initial status. I can do this for all the fields except the ClientPeoplePicker one.
Following is the current handler I am using for the button click event:
protected void ClearBtn_Click(object sender, EventArgs e)
{
txtEmpFName.Text = string.Empty;
txtEmpEGN.Text = string.Empty;
dropDep.SelectedIndex = 0;
dtStartDate.SelectedDate = new DateTime(0);
PickerEntity picker = (PickerEntity)peoplePicker.ResolvedEntities[0];
SPUser userInstance = oSPWeb.EnsureUser(picker.Key);
userInstance.Name = string.Empty; // <------| This is my problem...
}
As a reference, this is the relevant code for the picker control:
<td>
<SharePoint:ClientPeoplePicker
Required="true"
ValidationEnabled="true"
ID="peoplePicker"
runat="server"
InitialHelpText="Please, insert a valid User!"
VisibleSuggestions="3"
Width="180px"
Rows="1"
AllowMultipleEntities="false"
CssClass="ms-long ms-spellcheck-true" />
</td>
Solution
PeoplePickerControlName.AllEntities.Clear();
OTHER TIPS
Just add this single line of code which will clear the PeoplePicker
field.
peoplePicker.CommaSeparatedAccounts = null;
You don't need below lines in your code to clear peoplepicker
PickerEntity picker = (PickerEntity)peoplePicker.ResolvedEntities[0];
SPUser userInstance = oSPWeb.EnsureUser(picker.Key);
userInstance.Name = string.Empty;
you can give a try
PeopleEditor.Accounts.Clear();
PeopleEditor.Entities.Clear();
PeopleEditor.ResolvedEntities.Clear();
works on fly.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange