Section 7.9.Program InfoPath in .NET


7.9. Program InfoPath in .NET

With 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 Toolkit

You 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.

To getLook here
.NET Framework 1.1 msdn.microsoft.com/netframework/technologyinfo/howtoget/default.aspx
Visual Studio .NET 2003 msdn.microsoft.com/vstudio/howtobuy/default.aspx
InfoPath SP1 previewSearch www.microsoft.com/downloads for "InfoPath SP1"
InfoPath 2003 Toolkit for Visual Studio .NETSearch www.microsoft.com/downloads for "InfoPath .NET"


7.9.2. How to do it

To create an InfoPath project with Visual Studio .NET:

  1. Choose File New Project and select InfoPath Form Template from the Microsoft Office InfoPath Projects group (Figure 7-42).

  2. Visual Studio starts InfoPath and creates a new project template in Visual Studio .NET as shown in Figure 7-44.

Figure 7-42. Creating a new InfoPath project


Figure 7-43. Specify a new template or use an existing one


Figure 7-44. The resulting Visual Studio .NET project


InfoPath 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 .NET



Note: 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 project


There 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 works

There are several things to note that are illustrated by this simple project:

  • You can use InfoPath Preview, as well as Visual Studio .NET's Debug Start (F5) to run the project.

  • The InfoPath template is stored as component files, not as a compiled template (.xsn). Those component files are listed in the Solution Explorer shown in Figure 7-44. If you close InfoPath, you can directly edit those files as XML within Visual Studio .NET.

  • If you place a breakpoint within cmdOK_OnClick , you can step through the code using the Visual Studio .NET debugging commands (unlike MSE).

  • Visual Studio creates two objects variables: thisApplication and thisXDocument, which you can use in place of the intrinsic Application and XDocument objects used in scripts.

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 deploy

The .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:

  • You can't sign the template since Visual Studio .NET stores the template as component files and InfoPath can only sign compiled templates (.xsn).

  • You can't install the template because you are still creating it, and you would need to repeatedly uninstall/reinstall the template every time you made a change.

To solve this problem, follow these steps:

  1. Close InfoPath and edit the template's manifest file ( manifest.xsf ) to remove the publishURL and trustSetting attributes and require full trust:

        <xsf:xDocumentClass       solutionVersion="1.0.0.5"       solutionFormatVersion="1.100.0.0"       publishUrl="C:\IPWork\FTNet\ manifest.xsf"            name="urn:schemas-microsoft-com:office:infopath:FTNet2:-myXSD-2004-04-    23T16-52-59"       productVersion="11.0.6250"       trustSetting="automatic"      requireFullTrust="yes"    

  2. Build the InfoPath project in Visual Studio .NET.

  3. Run the following script from within the project folder to register the template's manifest file:

     set ip = CreateObject("InfoPath.ExternalApplication") Set fso = CreateObject("Scripting.FileSystemObject") pth = fso.GetFolder(".").path ip.registersolution pth & "\manifest.xsf" set ip = nothing set fso = nothing

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:

  1. Run the following script from within the project folder to unregister the template on your development machine:

        set ip = CreateObject("InfoPath.ExternalApplication")    Set fso = CreateObject("Scripting.FileSystemObject")    pth = fso.GetFolder(".").path    ip.unregistersolution pth & "\manifest.xsf"    set ip = nothing    set fso = nothing

  2. From within Visual Studio .NET, choose Project Publish Form. Visual Studio .NET compiles the component files into a template (.xsn) and builds the assembly (.dll) for the project and starts the InfoPath Publishing Wizard.

  3. Run the signed template or install the unsigned template to test that the form runs in full trust.


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...

To learn aboutLook here
.NET programming with InfoPath msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_ip2003_tr/html/odc_INF_Lab_15.asp




    Excel 2003 Programming. A Developer's Notebook
    Excel 2003 Programming: A Developers Notebook (Developers Notebook)
    ISBN: 0596007671
    EAN: 2147483647
    Year: 2004
    Pages: 133
    Authors: Jeff Webb

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net