|
7.9. Program InfoPath in .NETWith the SP1 preview, Microsoft also released the InfoPath 2003 Toolkit for Visual Studio .NET. This toolkit is similar to the Visual Studio Tools for Office (VSTO) product covered in Chapter 6, but unlike VSTO, it's available as a free download for Visual Studio .NET users. If you are creating templates that require more than a little script, you should plan to program InfoPath in .NET. Visual Studio .NET is a professional programming tool that gives you far more assistance than MSE. Although the Visual Studio .NET environment is complicated, the command completion, syntax checking, and debugging tools make it a great deal easier to program with InfoPath. 7.9.1. Get the InfoPath .NET ToolkitYou must have the .NET Framework installed before installing InfoPath SPI in order to get the .NET Programmability features. If .NET is not already installed, the InfoPath setup will not be able to install the required components.
7.9.2. How to do itTo create an InfoPath project with Visual Studio .NET:
Figure 7-42. Creating a new InfoPath projectFigure 7-43. Specify a new template or use an existing oneFigure 7-44. The resulting Visual Studio .NET projectInfoPath and Visual Studio .NET are loosely coupled. You edit the template by switching to InfoPath. There, you can design the template's appearance. To write code, click Edit Form Code from control properties (Figure 7-45). Figure 7-45. Click Edit Form Code to add an event procedure in Visual Studio .NETNote: It's a good idea to create a new template in InfoPath first, then specify that template in Step 2. Creating the new template in InfoPath lets you use the Data Connection Wizard or base a new template on an existing one features which aren't available from Visual Studio .NET. Visual Studio .NET creates code templates for any event you create in InfoPath, as shown here ( bold code is mine): <InfoPathEventHandler(MatchPath:="cmdOK", EventType:=InfoPathEventType. OnClick)> _ Public Sub cmdOK_OnClick(ByVal e As DocActionEvent) ' Write your code here. thisXDocument.UI.Alert("This template is " & _ thisXDocument.Solution.URI) End Sub To run this code, switch back to InfoPath and click Preview. Visual Studio .NET builds the underlying assembly automatically and then previews the form (Figure 7-46). Figure 7-46. Use Preview (not F5) to run the InfoPath projectThere are other entry points for creating events from InfoPath. For example, you can select Tools Programming On Load Event... to create a form load event procedure in Visual Studio .NET. Or, you can add events from the validation dialog box by selecting an event and clicking Edit. Basically, all of the tasks that formerly started the MSE generate equivalent code in Visual Studio .NET. 7.9.3. How it worksThere are several things to note that are illustrated by this simple project:
It is fairly simple to convert InfoPath scripts written in VBScript to Visual Basic .NET. Mainly, you need to remember to add "this" before references to the XDocument and Application objects, and remove Set from object assignments (.NET doesn't use Set ). For example, the following changes convert some InfoPath VBScript to Visual Basic .NET: Dim x As DOMNode Set x = this XDocument.DOM.DocumentElement.SelectSingleNode("my:field1") x.text = "New value" In the preceding code, bold indicates additions and strikethrough indicates deletions. 7.9.4. Debug and deployThe .NET code running in an InfoPath project inherits its security settings from the InfoPath template. To get some insight into this situation, try to run this code in your new InfoPath project: Note: If you don't use level 3 methods or .NET methods that access the local resources, you don't need to worry about ensuring that your code runs in full trust. Domainlevel trust is fine. In practice, however, that is pretty limiting. thisXDocument.Save( ) InfoPath displays a security exception when you try to execute Save in preview mode. Save requires full trust, and preview runs in domain-level trust by default. Methods that require full trust are flagged in the InfoPath object model reference help as level 3 . If you use any level 3 methods, you need to sign or install the form template to ensure it runs in full trust. You face two problems trying to assign full trust to InfoPath templates run from within Visual Studio .NET:
To solve this problem, follow these steps:
Now, if you run the InfoPath project, the form previews running in full trust and the Save method (along with any other level 3 methods) will work. This procedure is for debugging only. Once you are ready to deploy the InfoPath project, follow these steps:
Note: The code to register and unregister the form is included with the sample project as the files Register.vbs and Unregister. vbs. 7.9.5. What about...
|
|