single person

Selecting a single person from Contact Selector

Hello folks,

One of the things that are important for most contact selectors, is the ability to select only a single person in it.

These are one of the things that can only be done through code, we’re gonna use the Validating event of the contact selector control.

Here are the steps in details:

  1. Right click the contact selector control, choose “Programming”, then choose “Validating Event”.

    Figure 1.1

    Figure 1.1

  2. Given that you have set your “Form template code language” to C#, Visual studio tools for office will create a C# project for you, needless to say this project will compile to a dll file which will be embedded automatically in your Form Template upon Publishing, so it has to be “Fully Trusted” and signed with a code signing certificate (creating a temp certificate from InfoPath’s “Form Option > Security and Trust” will do just fine for now).
  3. Now it’s time to dip into your code, insert the following code snippet in your ”gpContactSelector_Validating” event handler:
    if (e.Operation == XmlOperation.Insert)
    {
         if (e.Site.SelectChildren(XPathNodeType.Element).Count > 1)
         {
              e.ReportError(e.Site, false, "Invalid selection!", "Only one person can be selected!");
         }
    }
    
  4. And you’re done :), the result should look something like this:

    Figure 1.2

    Figure 1.2

Check back later for more tips and tricks, see ya 🙂

Advertisements