One of the things I have always been averse to doing is filling out forms. This is especially troublesome if you work in a big company where you have to fill out multiple forms just to purchase an item. Most of you have had this similar experience: first you have to fill out a physical form to get your superior's approval for an item, then you must fill out an online requisition order to procure the item. Wouldn't it be nice if you could simply fill out one form that would allow all other applications to use the data collected on that form?
Microsoft has realized this problem and presents its solution in InfoPath 2003, an application that comes with Microsoft Office 2003 Professional Edition. InfoPath 2003 allows you to build forms for data collection and then save them in the XML document format. With XML, different applications can then reuse the information without the need for users to enter the same data multiple times.
In this article, I will introduce you to the power of InfoPath 2003 and show you how you can build a simple form to start collecting data.
InfoPath 2003 provides several ways for you to define the types of data you want to collect:
For this article, I will illustrate the first method--using an XML Schema. Suppose I have the following XSD document:
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="Magazines" type="MagazineType"/> <xsd:complexType name="MagazineType"> <xsd:sequence> <xsd:element name="Magazine" type="MagazineDetails" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="MagazineDetails"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> <xsd:element name="Comments" type="xsd:string" minOccurs="0" maxOccurs="1"/> </xsd:sequence> <xsd:attribute name="Price" type="xsd:float"/> </xsd:complexType> </xsd:schema>
This XSD defines the structure of the information that I want to collect. In particular, I want to capture magazine information such as title, price, and publisher.
Using the XSD document, I want to build a form to collect information about the magazines that I have. So, let's launch InfoPath 2003 and go to File -> Design a Form.
You will see the Task Pane displayed on the right side of the window (see Figure 1):
You can create a new form from a Data source (such as from a schema, a database, or a Web service), and then either start a blank form, or extend a sample form (provided by InfoPath 2003). For this example, I want to create a new form from an XSD document, and so I select "New From Data Source".
In the Data Source Setup Wizard (see Figure 2), select the type of data source to which you are connecting. In my case, select XML Schema or XML data file. Click Next.
Browse for the XML Schema and click Finish.
In the Task Pane, you will be able to see the element and attribute names as defined in the XSD (see Figure 3).
To build the form for the user to fill in, click on Layout in the Task Pane and use the required layout controls.
First, let's add a "Table with Title" to our form (see Figure 4). I have also changed the shading and font color.
Figure 4. Adding a Table with Title to our form (You can click on the screenshot to open a full-size view.)
Then, click on Data Source in the Task Pane, and drag the Magazine folder into the Table (see Figure 5):
Figure 5. Adding a table to our form (You can click on the screenshot to open a full-size view.)
You will be prompted to select the kind of section you want to add. Select Repeating Table (see Figure 6):
You should see the layout, as shown in Figure 7:
You are now ready to publish the form.
To publish the form, go to File - > Publish.
The Publishing Wizard provides three ways to publish your form (see Figure 8):
In my case, I chose to publish it to a shared folder, c:\Magazine.xsn.
To use the form, the user simply needs to double-click on the .xsn file (in Windows Explorer) and InfoPath will launch the form. (In an Intranet setting, it would be useful to publish the form to a Web server and point the user to the URL to download the form.)
You can enter multiple magazine information by clicking on the arrow and selecting Insert Magazine above/below to add new records (see Figure 9):
Once you've completed entering the data, you need to save the form. To save it, simply click on File - > Save. All the magazine information will be saved in an XML document.
You can use NotePad to view the XML document you've generated:
<?xml version="1.0" encoding="UTF-8"?> <?mso-infoPathSolution solutionVersion="22.214.171.124" productVersion="11.0.5531" PIVersion="126.96.36.199" href="file:///C:\Magazine.xsn" language="en-us" ?> <?mso-application progid="InfoPath.Document"?> <Magazines> <Magazine Price="5.95"> <Title>SQL Server magazine</Title> <Publisher>Penton</Publisher> <Comments></Comments> </Magazine> <Magazine Price="4.95"> <Title>Web Techniques</Title> <Publisher>CMP</Publisher> <Comments>Renamed to ...</Comments> </Magazine> <Magazine Price="5.00"> <Title>Wireless and B</Title> <Publisher>Sys-Con Media</Publisher> <Comments></Comments> </Magazine> </Magazines>
Note that the XML document has two additional PIs (Processing Instructions) to invoke InfoPath 2003 when it is double clicked.
Since the information is now saved as XML, other applications can then easily use the information in the document.
Besides filling in the form and saving the data as XML, InfoPath 2003 also allows you to print the form. This is especially useful when you need to generate a hard copy of your form to sign. You can preview the form by going to File - > Print Preview (see Figure 10):
Figure 10. Viewing the Print Preview (You can click on the screen shot to open a full-size view.)
InfoPath 2003 is a useful tool for collecting data, especially in organizations where there is a need to integrate several applications together. In this article, you have seen how InfoPath 2003 works and how to build a very simple form to collect data and save it as an XML document. If you have any questions, feel free to use the TalkBack section below to send them to me.
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to the Web Development DevCenter.
Copyright © 2009 O'Reilly Media, Inc.