How to: Preview and Debug InfoPath Managed Code Form Templates

Microsoft Office InfoPath 2007 with Microsoft Visual Studio Tools for Applications (VSTA), Visual Studio 2005 with Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System, and Visual Studio 2008 with Visual Studio Tools for Office enables debugging by running form code in preview mode. When you start debugging form code, your project is compiled and InfoPath displays your form in the InfoPath preview window. When a line of code that has a breakpoint set for it is encountered, the focus moves to the code editor. When you continue past a breakpoint, the focus moves back to the preview window. Debugging stops when you close the preview window.

You can also modify the form options of the form template to preview and debug using a specific user role, a sample data file, or by specifying the domain to which the form will be published.

After setting breakpoints in your code and compiling your form template, you can open it in editing mode, and attach to the InfoPath process in Visual Studio and debug your form template at run time.

NoteNote:

It is not possible to debug form templates at run time from VSTA. This includes form templates that are compatible only with InfoPath, as well as those that are compatible with InfoPath and InfoPath Forms Services.

For information on debugging form template business logic deployed to InfoPath Forms Services from Visual Studio, see How to: Debug Browser-Enabled Forms Deployed on InfoPath Form Services.

Debugging in Preview Mode

To debug an InfoPath project in Preview Mode

  1. Create or open an InfoPath managed code form template in VSTA or Visual Studio.

  2. Set one or more breakpoints in your form code in the code editor by clicking the grey bar to the left of the line of code where you want to insert a breakpoint.

    A red circle is displayed and the line of code is highlighted to indicate that the runtime will pause at this breakpoint in your form code.

  3. On the Debug menu, click Start Debugging; or press F5.

    The project will be compiled and the form is displayed in the preview window.

  4. Interact with the form until a line of code containing a breakpoint is encountered.

    The focus returns to the code editor.

  5. On the Debug menu, click Continue; or press F5.

  6. When you are finished debugging, close the preview window; or on the Debug menu, click Stop Debugging.

NoteNote:

To debug an InfoPath managed code form template when using an object model member that requires full trust, you must configure your form template as described in How to: Preview and Debug Managed Code Form Templates that Require Full Trust.

Using a Sample Data File

By default, debugging and previewing uses the template.xml file that is created when a form template is created. You can create your own data file and specify to use it when previewing or debugging by using one of the following procedures.

To specify a sample data file to use while debugging or previewing in VSTA

  1. To view template.xml, open the form template in InfoPath design mode.

  2. On the File menu, click Save As Source Files, save the form template files to a folder, and then open the template.xml file in a text editor.

  3. Create and save a file with the same structure as template.xml with the sample data you want to use.

  4. On the File menu, click Preview, and then click Preview Settings.

  5. Under Sample Data, specify your sample data file

To specify a sample data file to use while debugging or previewing in Visual Studio

  1. To view template.xml, open the form template project in Visual Studio.

  2. Close the manfest.xsf [Design] tab.

  3. In the Solution Explorer toolbar, click Show All Files, right-click template.xml, and then click Open.

  4. Select and copy the contents of the template.xml file.

  5. On the File menu, point to New, click File, and create an XML File.

  6. Paste the contents of template.xml into the new file, and add sample data into the file.

  7. On the File menu, click Save XMLFile1.xml As, and then save the file to the InfoPath Form Template folder for the project.

  8. Open manifest.xsf, click Form Options on the Tools menu, click Preview, and then specify your sample data file.

Specifying a User Role to Use While Debugging or Previewing

If the form you are working with has user roles defined for it, you can specify a user role to use while debugging or previewing your form. For information on how to define user roles, search InfoPath help for "user role".

To specify a role to use while debugging or previewing

  1. If you are working in InfoPath with VSTA, switch to InfoPath.

    If you are working in Visual Studio, click the manfest.xsf [Design] tab.

  2. On the Tools menu, click Form Options.

  3. Click Preview, and then specify the user role to use in the Preview as drop-down box.

Specifying a Domain to Use While Debugging or Previewing

You can preview a form as if it was published to a specific domain. This setting will only apply if the security level of the form template is explicitly set to Domain.

To specify a role to use while debugging or previewing

  1. If you are working in InfoPath with VSTA, switch to InfoPath.

    If you are working in Visual Studio, click the manfest.xsf [Design] tab.

  2. On the Tools menu, click Form Options.

  3. Click Preview, and then specify the domain to use while previewing and debugging in the Domain box.

  4. Click Security and Trust, clear the Automatically Determine Security Level check box, and then click Domain.

Debugging at Run Time

If you are developing your InfoPath form template in Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System, or Visual Studio 2008 with Visual Studio Tools for Office, you can debug the form template at run time using the following procedure.

To debug an InfoPath project at run time

  1. Open your form template project in Visual Studio, set one or more breakpoints in your code, and then compile and deploy your form template as described in How to: Deploy InfoPath Projects.

  2. In InfoPath, open the form template in editing mode.

  3. In Visual Studio, click Attach to Process on the Tools menu.

  4. In the Available Processes list, select INFOPATH.EXE, and then click Attach.

  5. Work with the form in InfoPath until a breakpoint is encountered, and then use the debugging tools in Visual Studio.

Show: