Export (0) Print
Expand All

Using the InfoPath 2010 Object Model and Visual Studio Tools for Applications

Office 2010

Summary:  Learn how to use the Microsoft Visual Studio Tools for Applications development environment to write managed-code business logic against the object model.

Applies to:  Microsoft InfoPath 2010
Published:  November 2010

Contents

Lab Objectives

After completing this lab, you will know how to:

  • Use the Microsoft Visual Studio Tools for Applications development environment that is integrated into InfoPath 2010.

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

  • Access the InfoPath object model by using form template business logic.

Lab Setup and Requirements

To complete this lab, you must have InfoPath 2010 installed with Visual Studio Tools for Applications.

The default installation of InfoPath does not include the Visual Studio Tools for Applications development environment.

To install Visual Studio Tools for Applications

  1. If you are installing InfoPath for the first time, select Customize. If you already installed InfoPath, rerun setup, and then select Add or Remove Features.

  2. Under Microsoft InfoPath, expand Microsoft InfoPath and .NET Programmability Support.

  3. Click the drop-down list next to Visual Studio Tools for Applications, and then select Run from My Computer.

Scenario

The IT department at Contoso is designing an order form. The developers plan to use the functionality that is provided by the Microsoft .NET Framework classes in the form. However, the developers will continue to use the built-in form-specific features of InfoPath 2010, such as data validation and conditional formatting.

Exercises

The following tasks will take you through the general processes of using the Microsoft Visual Studio Tools for Applications development environment to create a simple application.

Displaying Hello, World in C#

InfoPath 2010 provides an object model for writing business logic in managed code (Visual Basic or C#). In this exercise, you will learn how to use code to display a simple message dialog box. The first step is to design a new form template.

To design a new form template

  1. Start InfoPath Designer 2010.

  2. On the File tab, click New, and then double-click Blank Form (InfoPath Filler) under Popular Form Templates as shown in Figure 1.

    Figure 1. Designing a Form Template

    Available form templates in InfoPath

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

  3. On the File tab, click Save, and then save the form as OM Lab.

After you have designed the form template, you should change the language to C#; while the Visual Basic language is valid, C# is the language this tutorial uses.

To change the form template code language to C#

  1. On the Developer tab, click Language.

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

  3. Click OK.

After changing the language to C#, you can add in a button and associated event handler to your form.

To insert a button and event handler

  1. On the Home tab, expand the Controls group, and then click the Button control to insert a button into your form template.

  2. Right-click the button, and then click Button Properties.

  3. In the Button Properties dialog box, change the Label field to Alert.

  4. Change the ID field to AlertID.

  5. Click Edit Form Code.

    Clicking Edit Form Code opens the Visual Studio Tools for Applications development environment window. This also adds an event handler for the Clicked event of the button to the FormCode.cs file.

  6. In the AlertID_Clicked event handler, add the following line of code.

    MessageBox.Show("Hello World!");
    

At this point, the code in the Code Editor should resemble Figure 2.

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

Code in Visual Studio Tools for Applications

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 Visual Studio Tools for Applications, on the Build menu, click Build OM Lab. In the status bar, you will see an indication that Visual Studio Tools for Applications has successfully finished the build.

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

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

    Figure 3. Previewing the form

    Preview view of the form template

  3. Click Alert in the previewed form to display the MessageBox that you added. Now, your form should resemble Figure 4.

    Figure 4. Displaying the Hello World MessageBox

    Hello World Message Box

    You have now created a basic Hello World application. In the next section, you will use C# to add additional functionality to your application.

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

Now, you will write managed-code business logic by using the InfoPath 2010 object model implemented in the Microsoft.Office.InfoPath namespace. Specifically, you will design an order form with a billing address and a shipping address. In addition, you will write code to insert the shipping address when the user indicates that the shipping address differs from the billing address.

The first step is to create a new form template, which you can do in InfoPath Designer 2010.

To design a new form template

  1. Start InfoPath Designer 2010.

  2. On the File tab, click New, and then double-click Blank Form (InfoPath Filler) under Popular Form Templates.

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

  3. On the File tab, click Save, and then save the form as OM Lab-ExecuteAction.

As with the previous template, you can change the code language to C#.

To change the form template code language to C#

  1. On the Developer tab, click Language.

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

  3. Click OK.

After changing the language, you can start designing the form template.

To design the form template

  1. On the Home tab, click the arrow icon in the lower-right corner of the Controls group to display the Controls tool pane.

  2. Under Containers in the Controls tool pan, drag a Section control on to the form.

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

  4. Drag a Check Box control to a position below the Section control.

  5. Drag an Optional Section control to a position 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

    Text Box control inside Optional Section control
  7. Click the Check Box control, and then click Changed Event on the Developer tab of the ribbon.

  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 user selects the check box, the code inserts the optional section by calling the ExecuteAction method. When the user clears the check box, the code uses the ExecuteAction method to remove the optional section.

You can determine the values to use for the xmlToEdit parameter of the ExecuteAction method and for the viewContext parameter of the SelectNodes method. To do so, right-click the Optional Section control, and then click Section Properties to display the Advanced tab of the Section Properties dialog box. If you followed Steps 2 through 6 in order, the Advanced tab will display the values that you used in the previous code example. Otherwise, the values might differ slightly. The advanced tab will resemble Figure 6.

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

Advanced Tab on the Section Properties dialog box

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

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

Your form code should now resemble the following example; your code may vary slightly based on the values of viewContext and xmllToEdit.

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");
         }
      }
   }
}

After you have designed the form template and added in the managed code, you can preview your work.

To preview this form

  1. In Visual Studio Tools for Applications, on the Build menu, click Build OM Lab - ExecuteAction.

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

  2. Change focus back to the InfoPath Designer.

  3. On the Home tab, click Preview, and you will see the screen shown in Figure 7.

    Figure 7. Previewing the new check box

    New check box

    Select the check box, and the new section will be inserted as shown in Figure 8. Clear the check box and the section will be removed.

    Figure 8. Selecting the check box

    Selected check box

Conclusion

After completing this lab, you should have a basic understanding of how to work with the InfoPath 2010 object model and Visual Studio Tools for Applications.

Additional Resources

Community Additions

ADD
Show:
© 2014 Microsoft