Search This Blog

Thursday, December 4, 2014

Add code to an InfoPath 2010 Form

Today I started an investigation on the changes between InfoPath 2007 development and InfoPath 2010 development.

I really hope that InfoPath 2010 will bring more functionality and development flexibility as it really is a great "quick-win" tool for business which is sometimes crippled by a lack of development richness.

So.. one of my first questions was.. how easy is it to develop code behind (create custom code) for an InfoPath 2010 Form.

This tutorial will show you that it is still extremely easy to accomplish adding custom code, and my initial impression is that for a developer things will only become easier when moving to InfoPath 2010.

Prerequisites:

1.  Make sure you meet the minimum system requirements (available here). 
2.  Install Visual Studio Tools for Applications (VSTA). 
3.  Choose a programming language (inside the InfoPath Forms Designer). 
4.  Add event handlers and code.

Let's get Started:
Open Microsoft InfoPath Designer 2010.
From the Form Templates Dialog, select "Blank Form".























Change the default Label on the form to something appropriate.
Since this is a first InfoPath 2010 tutorial, we cannot use anything else than "Hello World"
Click on the form and select the "Home" tab from the ribbon. Expand the controls pane to view the available controls.
Add a Button and a Text Box to your form under the "This is a 'Hellow World' label.

Your form will now look like this:












If you click on the 'Developer' tab in the ribbon, you will see you can se the programming language of the form.

When you click on the Language command you will see the 'Form Options' dialog with the 'Programming' section selected. In here click on 'Change Language' and specify the language of choice.

Once your development language has been specified you can go ahead to add custom code to your form.
In design mode, select the custom button on your form and you will notice the ribbon automatically changes to show 'Control Tools' pane.
Click on the Properties item and click on the 'Custom Code' command.

If your prerequisites were installed correctly, InfoPath forms designer will now open Visual Studio Tools for Applications and create a event handler for the button click event.
Inside the FormCode.cs you will see the following code:

public void CTRL1_5_Clicked(object sender, ClickedEventArgs e)

{
}

Add your custom code inside this event. I added some code to set the field value of the textbox (custom field) which I added earlier in this tutorial:

public void CTRL1_5_Clicked(object sender, ClickedEventArgs e)

{
    XPathNavigator domNav = MainDataSource.CreateNavigator();
    XPathNavigator field = domNav.SelectSingleNode("/my:myFields/my:field1", NamespaceManager);
    field.SetValue("Hello World");
}

Build your code and save the project.
Return to the InfoPath Forms Designer.
On the Home pane in the ribbon you will see on the far right the form preview command.
Click on this command to preview your form.

If you preview the form and click on the button you will see the custom code execute and add a value to the custom field which is then displayed in the textbox.
You can add breakpoints to your code to step through it if you want.

In the next post will I show how easy it is to publish the InfpPath form.

No comments:

Post a Comment