contact selector

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

Using Multiple Contact Selectors

First of all I would like to say that most credit goes to Scott Heim who wrote this great article earlier about using the Contact Selector.

With that said, now let’s get started on more tips and tricks on this userful control.

Now let’s see how our datasource looks like, after applying the steps in the Scott Heim’s guide.

Figure 1.1

Figure 1.1

Most people will want to use this control several times in their form. The problem is that InfoPath will not allow duplicate element names in the form’s datasource. Therefore, it is impossible to repeat the same datasource heirarchy which this control depends on.

Now to demonstrate how to overcome this problem, let’s check out the following steps:

  1. For the sake of the example, let’s say we have a repeating table, where I want each row to have it’s individual contact selector.
    We will start by creating the datasource for this table, you should have something looking like this:

    Figure 1.2

    Figure 1.2

  2. Now that our table datasource is ready to receive it’s ContactSelector.
    Rigth click on the original contact selector datasource “gpContactSelector” which we created earlier, and click Reference.

    Figure 1.3

    Figure 1.3

  3. Now choose the destination of the contact selector control reference.

    Figure 1.4

    Figure 1.4

  4. Almost done, now to test it, right click “grpRepeating”, and choose “Repeating Table”.

    Figure 1.5

    Figure 1.5

  5. You can see that the table was created but the last row has been rendered as a repeating section. Now right click the parent “Optional Section”, and choose “Change To”, then choose “Contact Selector”.

    Figure 1.6

    Figure 1.6

  6. And you’re done 🙂 , this should be your final result.

    Figure 1.6

    Figure 1.7

Please check back for more Tips & Tricks 🙂 See Ya!