Architecture - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Architecture arrow Designing Forms for Microsoft Office InfoPath and Forms Services 2007 (Part 2)
Designing Forms for Microsoft Office InfoPath and Forms Services 2007 (Part 2)
By DevSource

Rate This Article: Add This Article To:

Designing Forms for Microsoft Office InfoPath and Forms Services 2007 (Part 2)
( Page 1 of 2 )

This is the second in a series of excerpts from the book, Designing Forms for Microsoft Office InfoPath and Forms Services 2007. In this excerpt, the authors show you how to build reusable components.

Getting Started

At this point in the book, you not only know how to design form templates that look nice but you can also take advantage of most of the basic (and some not so basic) features available when using InfoPath in design mode. Now you can create professional-looking forms in no time. However, over time, as you create more and more form templates, you may find that many of them are very similar or share a lot of the same constructs.

ADVERTISEMENT

For example, several of your form templates may contain sections used to collect address information from the user. It would be pretty tedious to have to add the same address block to each of your form templates, especially if there is no difference in this data from one form template to another. You could simply copy the controls and layout from the view and paste them into another form template. This would work fine for the view aspects of the form. However, what about the data structure, rules, calculated default values, data validation, and data connection information that you may have added to your form template? How do you copy these settings from one form template to another? Using copy and paste, this just isn't possible.

There is another way to share different parts of a form template in multiple form templates. With InfoPath 2007, you can build custom components that can be reused in one or more of your form templates. This means that you can create your own components or reuse ones created by third-party software providers.

You can create reusable components in two ways. The first is to write ActiveX controls in C++, Visual Basic, C#, or any COM-aware programming language. Creating controls this way requires you to put on your developer hat and write quite a bit of code. Since this is an advanced topic, we'll talk about this in the advanced section of this book in Chapter 19.

Fortunately, there is a second way to create custom components in InfoPath, one that doesn't require you to write even one line of code. These custom components are called template parts. As their name implies, template parts are parts of a form template. More specifically, template parts group pieces of a form template into a component that can be reused in multiple form templates. These custom components, which include controls, data structure, and many of the design features you've learned about so far (such as rules and data validation), enable you to quickly and easily reuse common design features across as many form templates as you choose. Let's jump right in and see how easy it is to create one of these components.

Designing a New Template Part

Designing a template part is just as easy as designing a new blank form. In fact, you design a template part the same way you would design a form template. Let's look at an example. Let's say that you work in the IT department at the MOI Consulting Corporation. MOI has a mandate that all forms that collect address information should follow a standardized format and data structure for that part of the form. Figures 10.1 and 10.2 show the format and data structure, respectively. (These figures should look very familiar to you since this is the address block from the MOI employment application form we used in earlier chapters.) Until now, everybody at MOI who has created a form has either had to recreate this part of the form template from scratch or copy an existing form template and remove all the nonrelevant parts. Being the innovative form designer that you are, you figure out a way to make this process easier for everybody in the company--you create an address block template part. Let's look at the process for creating this component.

The first step (after starting InfoPath, obviously) is to open the Design a Form Template dialog as you normally would when designing a new form template. One thing you'll do differently, though, is to select the Template Part option at the top of the dialog, at which point the dialog changes to look like the one shown in Figure 10.3. Notice that the Based on section of the dialog looks a bit different. Only the Blank and XML or Schema options appear. This is because when designing a template part, you can either create a new blank template part or base it on an existing XML file or schema. It's not possible to base your template part on a Web service, database, or connection library as you can when designing a form template.

Since MOI Consulting has a required data structure for address blocks in forms, it seems natural that you would want to create your template part based on an existing schema. However, if you don't have an existing schema or you want to create one for your address block, you can create a new blank template part. So, let's select the Blank option from the Based on section of the dialog and then click the OK button. When you do so, InfoPath opens in design mode with a completely new template part that contains nothing in the view or data source. We'll add controls to the view next, which will also create the data structure we require for this address block template part.

Template Part Design Mode

When you open InfoPath in design mode when designing a template part, at first everything looks the same as it does when designing a normal form template. However, as you look around you will notice subtle differences. For example, if you look at the Design Tasks task pane (Figure 10.4), you'll notice that it doesn't contain all the design tasks that it normally does when you are designing a form template--the Views and Publish Form Template tasks are missing.

To understand the reason behind this difference, it's important to understand the concept of design targets. Whenever you open InfoPath in design mode, it can have one of two targets--form template or template part--which correspond to the option buttons with the same names in the Design a Form Template dialog. (Of course, in each case, you can specify that the form template or template part is browser-enabled. We will discuss this more in Chapter 14 when we talk about browser forms.) When you are targeting a form template, all the InfoPath design-mode features are available. When you are targeting a template part, only a subset of the design-mode features (those supported by template parts) are available.

For example, a template part can have only one view. So, the Views task pane is not available in the Design Tasks pane (or anywhere else, for that matter). As with the tasks in the Design Tasks pane, the entry points (e.g., menus, dialogs, and so on) for any features that are not available in template parts will be hidden. Table 10.1 lists all the features that are not supported by template parts and, therefore, won't be available in design mode when designing a template part.

Now that you've opened InfoPath in design mode to design a template part, the experience should be very familiar since you've designed a form template before. So, let's create our address block component. As we mentioned, the address block we want to create is the same one used in the MOI employment application form. So, to make things a little easier, let's just copy all the controls and layout from the view in the employment application form and paste them into the view of the template part we are creating. To do this, open the employment application form we created back in Chapter 2 (which is available with the samples for Chapter 2 on the book's Web site), select the Address Section control by clicking the design-time visual tab for the Section control and then press Ctrl+C to copy it. Then go to the template part and paste the controls and layout into the view for the template part.

After you paste, you'll notice that all the controls have error design-time visuals, as shown in Figure 10.5. This is because when you copy controls from one form template and paste them into another, only the view features are copied. None of the data bindings are preserved. (In fact, none of the data-centric features, such as data validation, calculated default values, and so on, are copied.) However, this is pretty easy to fix. First, you have to create the data structure shown in Figure 10.2. (If you need a refresher about how to create the data structure manually, see Chapter 3.) Once the data source is created, all you have to do is right-click on each control and choose the Change Binding option. Then from the binding dialog, choose the node to which you want to bind each control. (Of course, as you learned earlier, you can also create the data structure right from the binding dialog.)

Once you've changed the bindings for each of the controls, you have a fully functional template part that you can use in any form template. But before you can reuse this component in other form templates, you have to save it. In terms of what you do, saving a template part is no different than saving a form template. However, when you save, InfoPath will give the template part the .xtp extension instead of the .xsn extension used for form templates. This will be important later when you try to locate template parts to reuse. When you save the template part, the name you give the file will be the name you'll use when you install the template part into your Controls task pane, which we'll show you how to do next.

You've created your first template part, so let's put it to use. (In the last chapter, you learned about publishing form templates. You don't actually publish template parts as you do form templates. Saving a template part is analogous to publishing it in this case.) *** END OF PART 1 ***

Using a Template Part

Once you have a template part--whether you created it yourself as we just did or you are reusing one created by somebody else--you can add it to your Controls task pane so that you can use it just like any of the built-in InfoPath controls. Let's look at how you can install template parts so they can be inserted into your form templates.

Adding a Template Part to the Controls Task Pane

Reusing the address block template part in your own form template is easy. First you must create a new form template or open an existing one in design mode. Then open the Controls task pane. At the bottom of the task pane is a link titled Add or Remove Custom Controls. Clicking on this link opens the Add or Remove Custom Controls dialog (Figure 10.6). From this dialog, you can add or manage all your custom controls, whether they are template parts or ActiveX controls. If you've never added a custom control before, this dialog will be empty. However, once you add a control or two, each control name will appear in the Custom controls list box. (Figure 10.6 shows the Add or Remove Custom Controls dialog after adding the AddressBlock template part.)

In order to add a custom control, just click on the Add button, which will start the Add Custom Control Wizard. When you first start the wizard, the Select a Control Type page appears (Figure 10.7), from which you choose the type of control you want to insert into the Controls task pane.

As we've mentioned, InfoPath supports two types of custom controls--template parts and ActiveX controls. Since we are going to add a template part (and this option is the default), click the Next button, which will open the Select an InfoPath Template Part page (Figure 10.8). From this page you can select which template part you want to install. Clicking on the Browse button will open the typical Browse dialog that we all know and love. Select a template part from your local machine, a shared location, a SharePoint site, or any Web site you choose. As we mentioned, template part files have an extension of .xtp, so this dialog will show only those types of files by default.

Once you choose the template part you want, click the Open button in the Browse dialog, and then click the Finish button after the dialog is closed. The Custom Control Added wizard page appears (Figure 10.9). As you can see, template parts have an icon and a version number associated with them. InfoPath automatically sets the version number and increments it each time you save the template part. We'll show you a little later in this chapter how to change the icon for the template part.

Once you click the Close button on the Custom Control Added page of the wizard, the name of the template part is added to the list of custom controls in the Add or Remove Custom Controls dialog, as we shown earlier in Figure 10.6. Once you close this dialog, the template part is added to the Custom category of the Controls task pane (Figure 10.10). Once the template part appears in the Controls task pane, you can insert it into your form template just like any of the built-in controls supplied by InfoPath.

NOTE Specifying an Alternate Location for Template Parts

When you add a template part to your Controls task pane, the component is installed on your computer in the following directory on Windows XP: C:\Documents and Settings\\Local SettingsApplication Data\Microsoft\InfoPath\Controls (where C:\ is your system drive and is your login name). When you start InfoPath in design mode, it loads all the template parts from this location into the Controls task pane.

However, what if you want InfoPath to automatically retrieve template parts from an alternate location? Those who are designated as the administrator for your computer (which might be you) can create a registry key that points to an alternate location for template parts. This registry key is HKEY_CURRENT_USER\Software\MicrosoftOffice\12.0\InfoPath\Designer. Create a string value called IPCustomControlsFolder under that key that points to the alternate location from where InfoPath should load the template parts.

There are a couple of things to note about this, though. First, this path can be only to a folder on your local machine or on a network share. Internet URLs won't work. However, if you want to place your template parts on a Web site, you can use folder mirroring to create a share that points to an Internet address. (You can do this by creating a new Network Place in Windows that points to the URL.) Second, if any of the template parts in the alternate location have the same template part ID as one already installed on you computer, each time you open InfoPath in design mode you will receive an error stating that the template part is already installed. In that case, the template part(s) from the alternate location will not be installed in the Controls task pane.



 
 
>>> More Architecture Articles          >>> More By DevSource
 



DevSource video
Devsource Video Series
Manipulating Society through Technology
Jeremy Bailenson, Director of the Virtual Human Interaction Lab at Stanford University, talks about virtual reality, avatars, Moore's law, how real world behaviors influence online reality, and societal manipulation through technology!
>> Play video
>> Read article
>> See all videos
DevLife Blog

Julia explores the Robotics Studio! (It's for more than you think.)

MSDev Blog

Messages for Bill Gates!

Make it Work
.NET makes runtime type checking a breeze. See what Peter has to say about it in this week's tips!
News
Microsoft Counts on App Support for Vista
Microsoft has taken pains to demonstrate that Windows Vista will have ample application support.
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.