One of the issues my team noticed on one of the recent projects is that SharePoint 2010 people editor control doesn’t get cleared on a post back when instructed to do so.
SharePoint People Editor in my case is defined in a visual web part using the following way:
<SharePoint:PeopleEditor id="PeopleSelector" MultiSelect="true" ValidatorEnabled="true" SelectionSet="User,SPGroup" runat="server"/>
To clear the people editor, you would invoke the following code in your event handler, in my case a button:
PeopleEditor picker = PeopleSelector; picker.Accounts.Clear(); picker.Entities.Clear(); picker.ResolvedEntities.Clear();
If you run above code in the click event of a button and step through it, when you check the values for each of the properties (Accounts, Entities, ResolvedEntities), they’re all cleared, but as soon as the code has executed and the page posts-back – you will see the people editor containing the same values as before you cleared it. We started noticing this behavior after SP2010 SP1 has been applied and even when we applied October 2011 CU, the issue still persisted.
When you look at the page source of the page where the people editor is rendering, PeopleEditor reveals the following, pretty complex, structure:
There are number of hidden variables, one of which is called hiddenSpanData, and contains resolved entities. Also, the DIV highlighted in the image above, contains all of the resolved entities and corresponding HTML to render them. Effectively, if you remove the contents of that DIV, the resolved entities will disappear.
This is exactly what we’re going to do, plus we’ll clear the hidden variable (hiddenSpanData) value to make sure the values are cleared in a helper containers and peopl editor structure has nothing to repopulate the people from.
Naturally, this is a workaround and not a fix to a people editor issue; the hope is that the issue to the people editor control will be resolved soon so when the object properties are cleared, they propagate down and do not re-appear on the page.
For this workaround, we’ll add the following JavaScript code to the top of the page. This code will trigger every time the page refreshes; this means, if you need to trigger it on button click, ensure you call a clearPicker function from below on OnClientClick event of your button or means similar to this.
<script language="javascript" type="text/javascript">
// this registers clearPicker to run every time the page reloads
_spBodyOnLoadFunctionNames.push("clearPicker");
// this is the function that clears the picker and helper variables
function clearPicker() {
var control;
var arr = document.getElementsByTagName("div");
for (var i = 0; i < arr.length; i++) {
if (arr[i].id.indexOf("upLevelDiv") > 0)
{ control = arr[i]; }
}
control.innerHTML = '';
arr = document.getElementsByTagName("input");
for (var i = 0; i < arr.length; i++) {
if (arr[i].name.indexOf("hiddenSpanData") > 0)
{ control = arr[i]; }
}
control.value = '';
}
</script>
That’s it; the upLevelDiv and hiddenSpanData now contains no resolved values and the people editor appears cleared.
Enjoy!
















