608 18.8 Adding Code to a Form Template
18.8 Adding Code to a Form Template
InfoPath 2007 provides integration capabilities with Visual Studio to allow
you to write managed code for your browser-enabled form templates. Visual
Studio 2005 is the primary development environment when it comes to
extending the SharePoint functionality; therefore, it makes sense that this
becomes the standard development environment for adding code to InfoPath
form templates. Visual Studio Tools for Applications (VSTA) is tightly inte-
grated with InfoPath and allows you to add code to your form within the
confines of the InfoPath client. VSTA is installed as an optional component
as part of the InfoPath 2007 client installation. If you already have InfoPath
2007 installed and are unsure if VSTA is installed, you can check quickly by
clicking the Programming option from the Tools menu and then clicking the
Loading Event option. Either the VSTA environment will launch, or you will
be presented with an error message indicating that VSTA is not currently
installed. Figure 18.43 shows the VSTA environment presented when the
user clicks the aforementioned Loading Event menu option. Notice that the
code is set by default to Visual Basic. If you would prefer to code in C#, you
can change the language preference from the Programming category on the
Form Options dialog (Figure 18.44). This setting should be defined prior to
launching VSTA for your form template.
The VSTA environment gives you access to the InfoPath object model,
in addition to key development tools such as a debugger, IntelliSense, and
the object browser. You can think of VSTA as bringing Visual Studio 2005
into the InfoPath 2007 client. It contains subset of the features that Visual
Studio 2005 is equipped with, but at the same time provides the fully func-
tional InfoPath client.
Another option for developing code for your form templates comes in
the form of Visual Studio 2005 Tools for Office Second Edition (VSTO SE).
You can think of VSTO SE as bringing the InfoPath 2007 client into Visual
Studio 2005. It contains a subset of the features with which InfoPath 2007 is
equipped but at the same time provides the fully functional Visual Studio
2005 environment. For example, if you want to preview your form, then you
would still need to use the InfoPath client. Figure 18.45 shows an example of
creating a new InfoPath form template with the Visual Studio 2005 inte-
grated design environment. A major advantage of developing your form tem-
plates in this manner is that you are able to view multiple windows at one
time—for example, you are able to view both the designer window and the
code window. The experience is very similar to the SharePoint Designer 2007
development experience.
Let’s take a quick look at how easy it is to attach code to your form tem-
plate. In this example, we’ll use VSTO SE to create a very simple form tem-
plate that contains two textbox controls, txtName and txtHours. We will use
18.8 Adding Code to a Form Template 609
Chapter 18
Figure 18.43
Visual Studio for
Tools environment.
Figure 18.44
Changing the
language setting.
610 18.8 Adding Code to a Form Template
code to automatically populate the txtName control with the name of the
currently logged on user; that will be the extent of our demonstration. The
purpose of this exercise is to walk you through using the VSTO SE develop-
ment environment and show you what additional steps you need to take
when deploying your form template. For example, because your form tem-
plate contains associated code, the security of the form needs to be set to Full
Trust. Full Trust requires the form template to be deployed by an administra-
tor and also requires that the form be digitally signed with a certificate. We’ll
walk you through this process.
If you have not already done so, now would be a good time to install
VSTO SE. VSTO SE can be downloaded from the Microsoft Web site, and
is installed by simply double-clicking the setup file on the system in which
you have both Office 2007 and Visual Studio 2005 installed. VSTO SE
needs to be installed after the InfoPath 2007 client; otherwise, Visual Studio
will not be able to present all the InfoPath 2007 project templates. Another
factor to keep in mind when installing VSTO SE is that if you have two ver-
sions of Office running side by side on the same machine upon which you
wish to install VSTO SE, the installation will fail. To resolve this issue, you
must remove the earlier version of Office (for example, Office 2003) prior to
installing VSTO SE.
Step 1: Attaching code to a form template
1. Launch Visual Studio 2005
Figure 18.45
Visual Studio 2005
Tools for Office
second edition.

Get Microsoft SharePoint 2007 Technologies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.