CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

A Simple InfoPath Web Viewer Control

Here at the Port, we recently needed to allow different people in the company to quickly publish forms to our public web site. When I was tasked with making this happen, my first thought was to create a simple web-based tool for form creation.  Now, I've done this before, and creating a web-based form designer is no easy task, so  I started thinking of other options.  I could let them use FrontPage to create the forms, and then I could integrate the form into our site.   Honestly, this seemed like it would be more trouble than it would be worth.  Using FrontPage isn't a skill that most power users have.   On top of it, FrontPage doesn't create XHTML, and I wanted to end up with a document that I could parse if I needed to.  On top of all that, I remembered that I really dislike FrontPage...

Then I remembered that everyone here has Microsoft InfoPath installed.  I figure it's only a matter of time before people learn to use it, and in my opinion, it's easier to design a form in InfoPath than FrontPage.  The problem is that putting InfoPath forms on the web isn't so simple.  The end user has to have InfoPath installed in order to fill out an InfoPath form.  Since we wanted to publish the forms to our public site, this wasn't going to be an option. 

So, I came up with a quick solution that solved my problem.  I created a simple InfoPath web viewer that renders the InfoPath form as an HTML form, then takes the results from that form and creates a DataSet.  The resulting DataSet can then be bound to a WebControl, stored in a database or as stored as XML.

The way this all works is simple.  InfoPath has an option to “Extract Form Files.”  This creates, among other things, an XML and XSLT file that can be used to generate the UI of the form. I created a control that transforms this into XHTML, and then does some massaging using a NodeReader and some Regex to create HTML form input elements.

So, here's what an example form looks like in InfoPath:

...and here it is displayed by my InfoPath web viewer control:

You can download the ASP.NET C# project here.

Now there are a couple of big caveats to this control:

  • It doesn't integrate the results back into infopath format.  I'd rather have a DataSet, so that's where the results end up.
  • I only coded Text Areas, Text Boxes, Radio Buttons, and Drop Down Lists. Infopath has a number of other controls, that you can drop on the form.  I didn't need them immediately, so I didn't code support for them.
  • I didn't implement any form validation..

Now, if you peek at the code, you'll see that I used a combination of a NodeReader and REGEX to make this work.  A better solution may be to create a single XSLT file that does this work, but I'm not that good with XSLT syntax, so I didn't go down this route.   Anyhow, try it out!

-Brendan

 



Comments

Brendan Tompkins said:

Info View Pricing (US $)

Servers Pricing
1
$7,000

2-3
$6,000

4+
$5,000

-B



# May 24, 2004 6:15 AM

JosephCooney said:

This is cool. When I first heard about infopath I wondered if it was just "IE under the covers". I mean, the "custom" code that you wrote for it was in jscript/vbscript and it was all xml driven. Recently the update has been released that allows you to write managed custom code etc, so that de-bunks my theory a bit. Authentic from Altova (who make XML spy) is similar to infopath but it has a asp.net server control available as well. I always thought it was a shame that Microsoft did not provide similar for InfoPath.
# May 24, 2004 3:34 PM

Alex Irazabal said:

I downloaded the example but could find not way to see if and how it worked! What must you do to activate the WebControl in Infopath? IS there some documentation you can point me to?
Thanks,
Alex
# June 29, 2004 7:09 AM

Brendan Tompkins said:

This is an ASP.NET application that will load and view an InfoPath form.
# June 29, 2004 7:10 AM

Alex Irazabal said:

Sorry to be so obstuse - but how does it load it? When I try to "run" it wants me to select "an initial web page"?
# June 29, 2004 7:14 AM

Noy said:

Hi,
What about repeating section and optional section? is it coded to?
# July 4, 2004 3:02 AM

Hans said:

Throw overboard infopath,maybe within 3 years it will be really implemented.
PDF writer does everything better,faster,eaysier,and much mor features !
and everyone has acrobat to fill in and more.
# January 8, 2005 2:04 AM

Enrique Ortuño said:

Great work!
# January 13, 2005 11:49 PM

mrazek said:

Hi,
I try it use this contol for Infopath form where I use combo with connection to MS SQL database (to view data to combo) but withou success. Can I use this Infopath contol for this example ?

Thanks
# May 12, 2005 7:26 AM

Brendan Tompkins said:

This post is a bunch of things, wrapped up in one.  First and foremost, it’s my Official entry...
# June 30, 2005 6:48 PM

Brendan Tompkins said:

This post is a bunch of things, wrapped up in one.  First and foremost, it’s my Official entry into...
# June 30, 2005 6:52 PM

Brendan Tompkins said:

This post is a bunch of things, wrapped up in one.  First and foremost, it’s my Official entry into...
# June 30, 2005 7:00 PM

Brendan Tompkins said:

This post is a bunch of things, wrapped up in one.  First and foremost, it’s my Official entry into...
# June 30, 2005 7:04 PM

Brendan Tompkins said:

This post is a bunch of things, wrapped up in one.  First and foremost, it’s my Official entry into...
# June 30, 2005 7:13 PM

Brendan Tompkins said:

This post is a bunch of things, wrapped up in one.  First and foremost, it’s my Official entry into...
# June 30, 2005 7:17 PM

Brendan Tompkins said:

Earlier today I was inspired by Jeff Atwood’s blog post on Software Rot:

Coding Horror: The...
# July 5, 2005 2:18 PM

Brendan Tompkins said:

Earlier today I was inspired by Jeff Atwood’s blog post on Software Rot:

Coding Horror: The Broken...
# July 5, 2005 2:19 PM

Brendan Tompkins said:

Earlier today I was inspired by Jeff Atwood’s blog post on Software Rot:

Coding Horror: The Broken...
# July 6, 2005 6:55 AM

David said:

InfoJet Service is a Java class library to publish InfoPath Forms to the Web. InfoJet Service can be deployed as Web Services. J2EE and .Net Web application with integration of InfoJet Service support the users to edit InfoPath Forms in Internet Explorer and FireFox.

http://www.infojetsoft.com

thanks
# July 11, 2005 7:48 AM

Raphael Londner said:

# July 20, 2005 3:09 AM

Raphael Londner said:

# August 4, 2005 4:44 AM

Le poste de travail, un gisement de productivit said:

# September 3, 2005 12:47 PM

Jesús said:

This is very cool... thanx...
# September 8, 2005 6:44 PM

Le poste de travail, un gisement de productivit said:

# September 16, 2005 12:51 PM

U Rule said:

Very good saves us money using InfoView.
# October 6, 2005 9:01 AM

Fabio Almeida said:

Hi Brendan,
I'm testing the InfoPathControl to use it in my company. But I'm having problems to submit my InfoPath Form to a SQLServer DataBase when I fill it by IE in a WebApplication. Maybe because I don't have pratice with C#.
Would You pelase help me in doing that ? Is it using DataSet Class ?

Thanx
Fabio - from Brazil
# October 10, 2005 10:20 AM

Alessandro's Blogs said:

# November 5, 2005 2:01 AM

Alessandro's Blogs said:

# November 5, 2005 2:04 AM

Le poste de travail, un gisement de productivit said:

# November 15, 2005 1:52 AM

azert said:

erer
# June 11, 2006 10:50 AM

corey said:

Infopath should just be the form design component for frontpage. The fact that you can't publish to DHTML is ridiculous. Who will use this product, if you actually have to buy another license of Infopath just to fill the stupid form in? What if you wanted to use this for an order form, you make all your customers buy Infopath? That's a great business model... I can't believe MS has sold a single copy of this
# July 20, 2006 4:50 PM

Vipul said:

THis control is not confortable with following infopath Control:

Picture

Optional Section

Repeatig Section

Link Picture

Expression Box

If you have solution with this, pls send me

# December 27, 2006 3:21 AM

Troubled said:

I have been trying to create a rich text box that has text wrapping and does not expand the textbox. The textbox has enough space for 3 lines of text, but when you press enter the textbox expands and throws off the pagination of the whole form. I am using InfoPath with InfoJet. Do you know of a solution that could help?

I've tried show scrollbars when necessary- doesn't work.

# February 13, 2007 2:28 PM

David said:

I am new at ASP .NET and I downloaded this project to test it ou.  I was able to succesfully transforrm the xml schema/files to a ASP .NEt application.  I then saved the file as HTMl file to post on the website.  if anyone cares to know, here are the steps to follow:

1. Open Visual Studio, click open file, find the C# sharp project from this website and select InfoPathControl folder.  Click open.

2.  Look in InfoPathDoc file in the Visual Studio browser. This folder contains the xml schema or files to be transformed and viewed as ASP . NET project.  Of course this is where you would place the files you would like convert.

3.  Once the files are loaded as explained in 2, then proceed to run the project (F5, or green arrow).  The transformation should take a short time to complete, in my test I only tried a couple of pages length form.

4.  Once the form is displayed in IE browser (i did not test in different browser), then save the result as an html, and place it where it will be exposed to users.

As already mentioned by Brendan the author there are some important drawbacks.  In the forms i tested the repeating rows tag is simply displayed as text tag without functionality, some pictures did not display at all, and some drop down menues did not get populated.

Goind forward i have heard that InfoPath 2007 wil have this feature/ability of creating forms ONCE for the web browsers while creating the InfoPath version of it.  Development shoul be shorter as well as maintenance.

# June 25, 2007 12:13 PM

Dani said:

Can someone point me in the right direction on how to submit the information collected in html format to the InfoPath server?  I have the collected data in html format but i need to connect and submit the data, how do i do that?

# June 26, 2007 10:04 AM

mahesh said:

User will enter number in textbox that need to displayed in Fax formate Let us take i enter 060009999 that need to be displayed as 06-000-9999

# April 8, 2008 1:29 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Free Tech Publications