InfoPath 2007

Microsoft Office InfoPath 2007

Date Template Part for InfoPath 2007

The date picker for InfoPath 2007 is just fine, except maybe when you want to select a birth date.
Then the user has to navigate back like so many clicks to get the year / month he was born into.

Thus when selecting a birth date, there is nothing better that the standard 3 drop down lists.

However InfoPath 2007 does not provide such a control to validate the entered date automatically.
So you would have to go through alot of trouble to create multiple rules / validations to get the desired effect.
For example, if the user selects 30th of February at any year, this should not be valid, also if the user selected 29th of February in a non leap year, this also should not be valid.

I have created this template part to re-use whenever i want in such cases, it has all the validations already done.

You can download the template part from here: DateTemplateGroup

In order to use the template part in any form template, kindly follow these steps:

  • Download the template part from here
  • Extract it to a location of your choice
  • Open your form template in Design Mode
  • In the Design Tasks Panel, click “Controls” then “Add or Remove Custom Controls”

    Add Custom Control

    Figure 1.1

  • Click the “Add” button on the “Add or Remove Custom Controls” Window

    Add or Remove Custom Controls

    Figure 1.2

  • Choose “Template Part” and click “Next” on the “Add Custom Control Wizard”

    Add Custom Control Wizard - Template Part

    Figure 1.3

  • Browse to DateTemplateGroup.xtp file you extracted and click “Finish” then “Close”

    Add Custom Control Wizard - Browse

    Figure 1.4

  • Now the Date Template Group has been successfully added to your Controls, you can now drag one into your form.

    DateTemplateGroup

    Figure 1.5

  • The control looks a bit awkward in design mode, but it will look perfect in preview mode.

    Data Template Group Control

    Figure 1.6

Here is how it looks in Preview Mode

Date Template Group Control - Preview

Figure 1.7

As you can see the control will validate the date entered, and will only accept possible values. It is also browser-enabled so you can use it in InfoPath Forms Services just fine. You can also customize it in anyway you like in terms of the look and feel and the logic.

The logic of this control is pretty straight forward.
A “Year” and “Month” drop down lists to contain “Year” and “Month” Values, and 4 “Day” drop down lists, each for every possible list of days (1 ~ 28, 29, 30, 31).
Conditional Formatting has been performed on the 4 “Day” drop down lists, so that only one will show for any combination of “Year”/”Month”.
And Validations on the “Day” data source field validates the date correctness.

See you soon,

Hazem Elshabini

Advertisements

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 🙂

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!