Export (0) Print
Expand All

Lab 8: Using the InfoPath 2007 Object Model and Visual Studio Tools for Applications

Office 2007

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Learn how to use the Microsoft Visual Studio Tools for Applications (VSTA) development environment to write managed-code business logic against the Office InfoPath 2007 object model. (7 printed pages)

February 2007

Applies to:Microsoft Office InfoPath 2007

Contents

After completing this lab, you will know how to do the following:

  • Use the Microsoft Visual Studio Tools for Applications (VSTA) development environment that is integrated into Microsoft Office InfoPath 2007.

  • Access the Microsoft .NET Framework classes from form template business logic.

  • Use the InfoPath object model from form template business logic.

To complete this lab, you must have Office InfoPath 2007 installed with VSTA.

Before you can install VSTA, you must have the Microsoft .NET Framework 2.0 and Microsoft Core XML Services (MSXML) 6.0 installed on your computer. The .NET Framework 2.0 is available as an optional software update on Windows Update. MSXML6.0 is available for download from the MSDN Web site (see Microsoft Core XML Services 6.0).

The VSTA development environment is not installed by default when you choose Typical to install InfoPath. To install VSTA, you must select either Customize when you are first installing, or use Add or Remove Programs to update your Office or InfoPath installation to include VSTA. The option to install VSTA is available by expanding Microsoft Office InfoPath, .NET Programmability Support, and .NET Programmability Support for .NET Framework version 2.0.

The IT department at Contoso is designing an order form. They plan to use the functionality provided by the Microsoft .NET Framework classes in a form, while continuing to use the built-in form-specific features (data validation, conditional formatting) of InfoPath 2007.

Displaying Hello, World in C#

Office InfoPath 2007 provides a new object model for writing managed business logic. In this exercise, you will learn how to use code to display a simple alert dialog box.

To design a new form template

  1. Start InfoPath.

  2. On the File menu, click Design a Form Template.

  3. In the Design a Form Template dialog box, under Based on, click Blank, and then click OK.

    This creates a new blank form template that is compatible with InfoPath.

    Figure 1. Designing a Form Template

    Design a Form Template dialog box

  4. On the File menu, click Save, and then save the form as OM Lab.

    NoteNote

    Click OK on the message about using the Publishing Wizard to publish the form when you are finished designing it.

To change the form template code language to C#

  1. On the Tools menu, click Form Options.

  2. In the Category list, click Programming.

  3. Under Programming Language, change the value in the Form template code language drop-down list from Visual Basic to C#.

  4. Click OK.

To insert a button and event handler

  1. On the Design Tasks pane, click Controls.

  2. Click the Button control to insert a button into your form template.

  3. Double-click the button to open the Button Properties dialog box.

  4. Change the Label field to Alert.

  5. Change the ID field to AlertID.

  6. Click Edit Form Code.

    This opens the VSTA development environment window and adds an event handler for the Clicked event of the button to the FormCode.cs file.

  7. In the event handler, add the following line of code.

    System.Windows.Forms.MessageBox.Show("Hello, World");
    

You should now see the following screen.

Figure 2. Adding code in the Visual Studio Tools for Applications Code Editor

Add Code in the Code Editor

You have completed your first InfoPath form template with managed-code business logic. Next, you can build and preview this form template.

To preview the form template

  1. In VSTA, on the Build menu, click Build OM Lab. In the status bar, you will see an indication that the build has successfully finished.

  2. Change focus back to the InfoPath form template designer, and then click Preview.

    The Alert Button that you created appears in the Preview, as shown in Figure 3.

    Figure 3. Previewing the form

    Preview the Form Template

  3. Click Alert in the previewed form to display the MessageBox that you added as shown in Figure 4.

    Figure 4. Displaying the Hello World MessageBox

    Display the Hellow World MessageBox

Using ExecuteAction to Insert and Remove a Section (C#)

Now you will write managed-code business logic by using the Office InfoPath 2007 object model implemented in the Microsoft.Office.InfoPath namespace. You will design an order form with a billing address and a shipping address. You will write code to insert the shipping address whenever the user indicates that it differs from the billing address.

To design a new form template

  1. Start InfoPath.

  2. On the File menu, click Design a Form Template.

  3. In the Design a Form Template dialog box, under Based on, click Blank, and then click OK.

    This creates a new, blank form template that is compatible with InfoPath.

  4. On the File menu, click Save, and save the form as OM Lab- ExecuteAction.

To change the form template code language to C#

  1. On the Tools menu, click Form Options.

  2. In the Category list, click Programming.

  3. Under Programming Language, change the value in the Form template code language drop-down list from Visual Basic to C#.

  4. Click OK.

To design the form template

  1. On the task pane, select Controls.

  2. Click the Section control.

  3. Drag a Text Box control inside the Section control.

  4. Drag a Check Box control and place it below the Section control.

  5. Drag an Optional Section control and place it below the Check Box control.

  6. Drag a Text Box control inside the Optional Section control, as shown in Figure 5.

    Figure 5. Creating the example form

    Create the Example Form
  7. Right-click the Check Box control, point to Programming, and then click Changed Event.

  8. Insert the following code in the field2_Changed event handler.

    if (e.NewValue.Equals("true"))
       CurrentView.ExecuteAction(
          ActionType.XOptionalInsert, "group2_2");
    else
    {   
       XPathNavigator node = 
          MainDataSource.CreateNavigator().SelectSingleNode(
          "/my:myFields/my:group2", NamespaceManager);
       CurrentView.SelectNodes(node, node, "CTRL4");
       CurrentView.ExecuteAction(
          ActionType.XOptionalRemove, "group2_2");
    }
    

When the check box is selected, this code inserts the optional section by calling ExecuteAction. When the check box is cleared, the code uses ExecuteAction to remove the optional section.

To determine the values to use for the xmlToEdit parameter of the ExecuteAction method and for the viewContext parameter of the SelectNodes method, display the Advanced tab of the Section Properties dialog box of the Optional Section control in InfoPath design mode. If you followed steps 2 through 6 in exact order, the values will be those used in the previous code example. Otherwise, they might differ slightly.

The advanced tab will look like Figure 6.

Figure 6. Verifying the values on the Advanced tab of the Section Properties dialog box

Verify the Values on the Advanced tab

In the previous code example, replace group2_2 with the value displayed for XmlToEdit for xOptional. Also replace CTRL4 with the value displayed for ViewContext.

To determine the value to use for the xpath parameter of the SelectSingleNode method, complete the XPath to the Optional Section control with the name of that control, for example, /my:myFields/my:group2.

You can see the name of the Optional Section control by selecting it in InfoPath design mode, as shown in Figure 5.

Your form code should now look like the following example (or vary slightly based on viewContext and xmllToEdit values).

using Microsoft.Office.InfoPath;
using System;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

namespace OM_Lab_Execute___Action
{
   public partial class FormCode
   {
      public void InternalStartup()
      {
         EventManager.XmlEvents["/my:myFields/my:field2"].Changed += 
            new XmlChangedEventHandler(field2_Changed);
      }

      public void field2_Changed(object sender, XmlEventArgs e)
      {
         if (e.NewValue.Equals("true"))
            CurrentView.ExecuteAction(
               ActionType.XOptionalInsert, "group2_2");
         else
         {
            XPathNavigator node = 
               MainDataSource.CreateNavigator().SelectSingleNode(
               "/my:myFields/my:group2", NamespaceManager);
            CurrentView.SelectNodes(node, node, "CTRL4");
            CurrentView.ExecuteAction(
               ActionType.XOptionalRemove, "group2_2");
         }
      }
   }
}

To preview this form

  1. In VSTA, on the Build menu, click Build OM Lab - ExecuteAction.

    In the status bar, you will see an indication that the build has successfully completed.

  2. Change focus back to the InfoPath designer.

  3. Click Preview, and you will see the screen shown in Figure 7.

    Figure 7. Previewing the new check box

    Select the Check Box on the Form

    Select the check box, and the new section will be inserted. Clear the check box and the section will be removed.

    Figure 8. Selecting the check box

    The Design a Form Template dialog box

After completing this lab, you should have a basic understanding of how to work with the InfoPath 2007 object model and VSTA. In Lab 9: Designing InfoPath 2007 Forms for Mobile Web Browsers, you can find out how to create a form template that can be filled out using a Web browser on a mobile device.

For more information about developing with InfoPath, see the following resources:

Show:
© 2014 Microsoft