1/05/2013

How To : Code in InfoPath Forms 2010


Hey Folks,

Le'ts learn little bit of InfoPath today!! :)

I will post the basic steps to learn InfoPath later, but today I am going to show how you can Code in InfoPath.

Before we start, make sure you have installed the prerequisites:

1.  Microsoft InfoPath 2010

2.  Microsoft Visual Studio 2010

3.  Visual Studio Tools for Applications (VSTA)

So Let's Start:

Open up InfoPath 2010 (Hope you know how to do it!! :P )

Go To File -> New -> Select Blank Form


Your new Blank Form will look like below, check out the demo as shown below:


Change the lable to This is a "Hellow World" InfoPath Form and add a Button control to the form:


Your form will now look like this:


Now Select Develop Tab and click on Language:


Select Category as Programming (if not selected) and choose your preferred programming language:


Click OK and close the form.

Now click on Code Editor Button in the Ribbon.

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.

public void CTRL1_5_Clicked(object sender, ClickedEventArgs e)
{
}

Add your custom code inside this event.

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.


0 comments: