SharePoint People Picker: Override Validation Logic
Tuesday, June 2, 2009 at 04:51PM If you are thinking about globally changing the behavior of SharePoint People Picker with custom validation logic, without going through the hassle of modifying every single page in SharePoint, then you are in the right place.
SharePoint application pages and user controls with a people picker declaratively uses the Microsoft.SharePoint.WebControls.PeopleEditor Web Control and sets the control’s properties to change its validation logic that meets specific requirements.
If you want to display only users that are members of a site collection or to customize the search filter for active directory, you might want to use STSADM to set people picker settings in a higher scope than setting the properties of an instance.
However, STSADM will not completely meet your needs, as you might have some random reason to have a custom logic:
-
You might want to restrict users of certain members of some special domain group from getting resolved.
-
You might want to make sure that the resolved users are in fact active (not disabled) account in active directory;
-
You might even want to resolve users from a data source that the people picker cannot reach OOTB.
I knew in my head that there is an elegant way to customize the validation logic of the SharePoint people picker and have the change propagates across a SharePoint Web Application. So, I kept asking my best friend Google, “globally replace a web control using the Web.Config”, “and please answer my prayers” (J Am telling you I couldn’t find a hint, maybe I should switch to Windows Live Search). And, then boom! Jonas Nilsson, one of my favorite mentors from Bamboo Solutions mentioned changing something in the Web.Config to redirect the declarative usage of a control to a new control. Yes! A couple phone later, I found out it was just two words, “Tag Mapping” (stupid me! I couldn’t even remember that?).
For whatever reason if you need to implement custom validation logic and have the effect to be global, overriding the PeopleEditor SharePoint control accordingly, and using Tag Mapping to redirect all declarative usage of the SharePoint People Picker control is inevitable. Tag Mapping (a new feature in ASP.NET 2.0) defines a collection of tag types to be remapped to other tag types at compile time. According to Nickil Kothari’s blog, It was originally invented as part of the Web Part framework so as to allow the ASP.NET WebPartManager to be mapped to the derived SharePoint WebPartManager implementation without having to change individual pages.
Long story short, you should just need to do the following:
-
1. Create a new class deriving Microsoft.SharePoint.WebControls.PeopleEditor
-
2. Override ValidateEntity(PickerEntity entity)
1: using SharePointControls = Microsoft.SharePoint.WebControls;
2:
3: namespace NaT.SharePoint.WebControls
4: {
5: public class PeopleEditor : SharePointControls.PeopleEditor
6: {
7: public override SharePointControls.PickerEntity ValidateEntity(SharePointControls.PickerEntity entity)
8: {
9: SharePointControls.PickerEntity validatedEntity = base.ValidateEntity(entity);
10:
11: if (validatedEntity.IsResolved == true)
12: {
13: bool isGoodToGo = true;
14:
15: //HERE, you can validate whether you want the entity to be resolved or not:
16: // - Check if user has a MOSS profile
17: // - Check if user is NOT disabled
18: // - User is not a denied user from the web application policy, etc, and set isGoodToGo accordingly
19:
20:
21:
22: if (isGoodToGo == false)
23: {
24: validatedEntity.IsResolved = false;
25: }
26: }
27:
28:
29: return validatedEntity;
30: }
31: }
32: }
- 3. Sign your assembly with a Strong Name
- 4. Install assembly into the GAC
- 5. Put tag mapping entry in the Web.Config
- 6. Add your Assembly in the assemblies section in the Web.Config (Thanks René) UPDATE 04-22-2010
1: <configuration>
2: <system.web>
3: <pages>
4: <tagMapping>
5: <add tagType="Microsoft.SharePoint.WebControls.PeopleEditor"
6: mappedTagType="NaT.SharePoint.WebControls.PeopleEditor"/>
7: </tagMapping>
8: </pages>
9: </system.web>
10: </configuration>
To Tag-Mapping,
NaT

