Tuesday, August 3, 2010

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.

Enjoy !!

16 comments:

Anonymous said...

I was interested in how you programmatically trigger a save (as opposed to a submit) from a web browser picture button using code.

I cannot see another post which looked as though it might help. Any ideas?

Ray said...

Do you have any ideas about the following error. This occurs when you use custom code in a form and you click on Preview Button.
"InfoPath cannot open the selected form because of an error in the form's code. Policy settings prevent opening Internet forms with managed code. To fix this problem, contact your system administrator."

Any helper will be appreciated
Greetings Ray

Anonymous said...

Ray, in case your're problem is not solved.
1. Go to file
2. Click on "Advance form Options"
3. From the form Option dialog select "Security and Trust" option
4. Uncheck "Automatically determine security level(recommended)" and select "Full Trust(the form has access to files and settings on the computer)"
5. Click ok

Hope this will solve your problem

Adi said...

Thanks! A quick and neat post to get started with a form with code behind.

Anonymous said...

Its really a nice blog post for better understanding of InfoPath getting started with code behind.
Thanks a lot.

Warm Regards,
Sandeep Parandekar

speedmoon said...

his form template requires administrator approval. To publish the form template, click the File tab, click the SharePoint Server button on the Publish tab, and select the Administrator-approved option in the publishing wizard.

Anonymous said...

Thanks. It worked for me.

--Raghav

Tanek Meyers said...

This post describe that it is still incredibly easy to achieve including customized rule, and my preliminary impact is that for a designer things will only become easier.

Jimmy Jarred said...

This article has proved to be of great help to me. It simply worked well and guided me about the basics changes between InfoPath 2007 development and InfoPath 2010 development. Thanks for this nice share.
infopath signing

Anonymous said...

Please give me the link for continuation of this post. How to publish the InfpPath form.

Luke Newey said...

Has anyone created a Get GPS button, that works with the built in GPS on a Motion Tablet?

Aeldra Robinson said...

This post describe that it is still incredibly easy to achieve including customized rule, and my preliminary impact is that for a designer things will only become easier.
infopath signing

Prologic Corporation said...

This is a good article & good site.Thank you for sharing this article. It is help us following categorize:
healthcare, e commerce, programming, multi platform,inventory management, cloud-based solutions, it consulting, retail, manufacturing, CRM, technology means, digital supply chain management, Delivering high-quality service for your business applications,
Solutions for all Industries,
Getting your applications talking is the key to better business processes,
Rapid web services solutions for real business problems,
Web-based Corporate Document Management System,
Outsourcing Solution,
Financial and Operations Business Intelligence Solution,

Our address:
2002 Timberloch Place, Suite 200
The Woodlands, TX 77380

prologic-corp

Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Web Development Company

Prologic Corporation said...

This is a good article & good site.Thank you for sharing this article. It is help us following categorize:
healthcare, e commerce, programming, multi platform,inventory management, cloud-based solutions, it consulting, retail, manufacturing, CRM, technology means, digital supply chain management, Delivering high-quality service for your business applications,
Solutions for all Industries,packaged applications,business applications, Web services, data migration
Business intelligence, Business Development, Software Development etc.


Our address:
2002 Timberloch Place, Suite 200
The Woodlands, TX 77380
281-364-1799

prologic-corp

Anonymous said...

Hello everybody,
I download a form from my sharepoint, but I dont have the .csproj file.Would I like to know if there is any way to access the code?

Post a Comment