Microsoft Architecture
Designing Forms for Microsoft Office InfoPath and Forms Services 2007 (Part 2) 2007-03-29
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( 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.
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
Template Part Design Mode
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.
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.
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\
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.
![]() |
|


