IWHOL308: Microsoft Office InfoPath 2007 Forms Development

Hands-on Lab Overview 1. Introduction IWHOL308: Microsoft Office InfoPath 2007 Forms Development Microsoft® Office InfoPath 2007 introduces new devel...
Author: Cory Hart
5 downloads 0 Views 820KB Size
Hands-on Lab Overview 1. Introduction

IWHOL308: Microsoft Office InfoPath 2007 Forms Development Microsoft® Office InfoPath 2007 introduces new development features that expand previous functionality and enhance the form design experience. The new Office InfoPath 2007 development environment and managed object model makes it easy for developers to build solutions using Visual Studio, and support for template parts in the designer allows developers to reuse sections across forms. In addition, the FormControl component provides developers the ability to host the InfoPath runtime in other custom applications, such as Windows Forms. 2. Technologies Highlighted • •

Microsoft Office InfoPath 2007 Microsoft Visual Studio 2005

3. Audience •

Microsoft Office InfoPath form developers

4. Scenario The lab exercises are as follows: • Getting Familiar with the Office InfoPath 2007 Managed Object Model • Using Template Parts • Hosting an InfoPath Form as a Windows Forms Control 5. Purpose of Hands-on Lab The objective of this lab is to become familiar with some of the new development features of Office InfoPath 2007. 6. Lab Notes All exercises use the new Code Snippet technology in Visual Studio 2005 to make lab code easily accessible for your convenience. Although using the snippets may simplify completing the exercises, you have the option of typing the code listed in the lab steps for a more realistic experience. In the code examples all code to be added by you will have an example shown in context with the added code appearing in bold.

You can insert code snippets several different ways in Visual Studio 2005. If you are not familiar with them, the lab machine simplifies it even further by adding the Insert Snippet command on the menu bar. Simply place the cursor where you are directed to insert the code and click Insert Snippet. Then, navigate to the appropriate snippet as directed by the lab instructions. For example, for the first code snippet used in Exercise 1, you should select CustomizingUI, followed by Exercise1, then Code1UsingStatements.

Figure 2.1. Inserting a code snippet

As a convenience if you are having problems with having the lab build properly there are snippets provided for each file that is modified in the lab that will put them in to their final state. To use these files you would first ensure that you were viewing the code in the Development pane and then press Ctrl-A to select all text. Then you would press the Insert Snippet button navigating to the proper lab and exercise where you should see listed a CodeComplete-Filename where Filename would be the file you are currently viewing. This lab will use several common terms to refer to different tool panes and sections of Visual Studio 2005. Below is a diagram of the Visual Studio application that has the positioning of the areas called out for easy reference.

1. 2. 3. 4. 5.

Development pane Solution Explorer Properties Window Toolbox Insert Snippet button

Hands-on Lab

Discussion Points Exercise 1 – Getting Familiar with the Office InfoPath 2007 Managed Object Model Microsoft Office InfoPath 2007 has introduced an improved Visual Studio development environment and a new object model to streamline the process of writing form solutions using managed code. Using Microsoft Visual Studio 2005, InfoPath functionality can be accessed directly from the managed code

Actions taken

editor. With new Visual Studio menus and a form design view, developers are no longer required to switch back and forth between Visual Studio and the InfoPath designer. Once inside the form code file, developers can make use of the functionality provided by the Microsoft .NET Framework 2.0 classes. The new Office InfoPath 2007 managed object model offers complete integration with System.Xml and a seamless development experience for .NET developers. In this exercise, you are first introduced to Office InfoPath 2007 in the Visual Studio development environment. Then, you will learn how to add event handler code stubs and managed code to your form solutions and become familiar with the Office InfoPath 2007 managed object model. You will start an Office InfoPath 2007 managed-code design session using Microsoft Visual Studio 2005 and explore the new development environment.

• Login using the following credentials: a. Username: administrator b. Password: pass@word1 c. Log on to: LITWAREINC • Wait for the Warmup Script to complete and the popup window to close. • Navigate to Start | Programs | Microsoft Visual Studio 2005 | Microsoft Visual Studio 2005. • Select the File | New | Project menu command. • In the New Project dialog box, expand the Visual C# project type and click Office. • In the Visual Studio installed templates section, click InfoPath Form Template. • Change the Name text box value to StatusReport. • Click Browse. • In the Project Location dialog box, navigate to C:\HOL\InfoPathDevelopment\Exercise1 and click Open. • In the New Project dialog box, click OK. • In the Design a Form dialog box, click Blank in the Based on section and click OK to design a new blank form.

Unlike the managed-code development experience of InfoPath 2003 SP1, with Office InfoPath 2007 you will be able to use Microsoft Visual Studio with InfoPath embedded in the development environment. As is the case with a custom Windows Forms application, developers will have access to an InfoPath form designer and a Toolbox for adding controls to the form view. Additionally, new menus provide access to programming events, resource files, and other form options. In the next set of steps, you will add a few controls to your form view using the Toolbox.

• • • •

In the form view, type Name:. In the Toolbox, double-click the Text Box control. Place the cursor on the empty line below the “Name:” text and type Date:. In the Toolbox, double-click the Date Picker control. The form view should appear as shown in Figure 8.1.

Figure 8.1. Adding Controls to the Form View • Optional: Add several more labels and controls to your form view. Before you start exploring the programming events of Office InfoPath 2007, you will need to make the form fully trusted. As a result of a bug in this beta version of Office InfoPath 2007, forms must be fully trusted and signed with a code-signing certificate in order to run with managed code.

• Select the Tools | Form Options menu command. • In the Form Options dialog box, click the Security and Trust category. • In the Security Level section, clear the Automatically determine security level based on form’s design (recommended) check box. • Click Full Trust (the form has access to files and settings on the computer). • In the Form Signing section, click Sign this form template. • Click Create Certificate… and click OK on the Microsoft Office InfoPath dialog. • The Form Options dialog box should now appear as shown in Figure 8.2.

Figure 8.2. Setting the Security Level to Full Trust • Click OK. Now that the security level for the form has been set, you can add event handler stubs to your form code file and program to those events. In the next few steps, you will add a Loading event to the solution and add code that sets the Name field to the value of the UserName environment property. Note that the Office InfoPath 2007 managed object model uses System.Xml instead of MSXML.

• Select the Insert | Loading Event menu command. • Insert code snippet InfoPathDevelopment > Exercise1 > Code1-SetUserName or type the following code in the FormEvents_Loading method under the //Write your code here. comment. public void FormEvents_Loading(object sender, LoadingEventArgs e) { // Write your code here. MainDataSource.CreateNavigator().SelectSingleNode("/my:m yFields/my:field1", NamespaceManager).SetValue(System.Environment.UserName); } • Optional: Type a period (.) after CreateNavigator() to review the complete list of

methods and properties that are available for the System.Xml CreateNavigator method. For those familiar with the list of methods and properties available to the MSXML DOM object, note the differences between the two lists. Remove the period before continuing. After you have modified your event handler, you will preview the form functionality. Unlike the InfoPath 2003 development experience, there are no issues with previewing fully-trusted forms in Office InfoPath 2007.

Select the Debug | Start Debugging menu command. If a warning message displays click OK to dismiss it. Note: The Name field value is pre-populated in the InfoPath preview. In Microsoft Visual Studio 2005, select the Debug | Stop Debugging menu command. • Close Microsoft Visual Studio 2005.

Now that you have some familiarity with event handlers in Office InfoPath 2007, you will add a Changed event handler to an existing managed-code solution for an issue-tracking form.

• • • •

Again, in order to add managed code to your solution, your fully-trusted form template must be signed with a code-signing certificate, as you did in Task 1.

• Select the Tools | Form Options menu command. • In the Form Options dialog box, click the Security and Trust category. • In the Form Signing section, click Sign this form and click OK.

With the form template signed, you will add a Changed event handler stub for the Priority drop-down list box. Within that stub you will add code that inserts or removes the Issue Description section based on the Priority value.

• Right-click the Priority drop-down list box and select the Programming | Changed Event menu command. • Insert code snippet InfoPathDevelopment > Exercise1 > Code2InsertDescription or type the following code within the Changed event handler below the Code2-InsertDescription comment.

• • • •

Open Windows Explorer. Navigate to C:\HOL\InfoPathDevelopment\Exercise1\IssueTracking. Double-click IssueTracking.sln. In the Solution Explorer expand the InfoPath Form Template and doubleclick manifest.xsf

public void Priority_Changed(object sender, XmlEventArgs e) { // Insert InfoPathDevelopment > Exercise 1 > Code2InsertDescription if (e.NewValue != "None" && e.OldValue == "None") { CurrentView.ExecuteAction(ActionType.XOptionalInsert, "Description_1"); }

// Insert InfoPathDevelopment > Exercise 1 > Code4RemoveDescription }

Note: For those familiar with the OnAfterChange event handler of InfoPath 2003, the Office InfoPath 2007 Changed event does not fire twice. As a result, it is not necessary to add a test for a specific event object operation. If the Priority value is changed to something other than None and was previously set to None, then the Issue Description optional section is inserted into the form. The ExecuteAction method is used to insert the optional section. The xmlToEdit parameter of the ExecuteAction method identifies the section that is to be inserted, in this case Description_1. To see where this value is defined, double-click the optional section control in the form design view, click the Advanced tab of the Section Properties dialog box, and note the value of the XmlToEdit for xOptional field.

• Insert code snippet InfoPathDevelopment > Exercise1 > Code3AddXpathDirective or type the following code at the top of the form code file below the Code3-AddXpathDirective comment. using Microsoft.Office.InfoPath; using System; using System.Windows.Forms; using System.Xml; // Insert InfoPathDevlopment > Exercise 1 > Code3AddXpathDirective using System.Xml.XPath; namespace IssueTracking • Insert code snippet InfoPathDevelopment > Exercise1 > Code4RemoveDescription or type the following code within the Changed event handler below the Code4-RemoveDescription comment. public void Priority_Changed(object sender, XmlEventArgs e) { // Insert InfoPathDevelopment > Exercise 1 > Code2InsertDescription if (e.NewValue != "None" && e.OldValue == "None") { CurrentView.ExecuteAction(ActionType.XOptionalInsert, "Description_1"); }

// Insert InfoPathDevelopment > Exercise 1 > Code4RemoveDescription if (e.NewValue == "None" && e.OldValue != "None") { XPathNavigator descriptionNode = MainDataSource.CreateNavigator().SelectSingleNode("/my:I ssueTracking/my:Description", NamespaceManager); CurrentView.SelectNodes(descriptionNode, descriptionNode, "CTRL16");

If the Priority value is changed to None and was previously set to something other than None, then the Issue Description optional section is removed from the form. Again, the ExecuteAction method is used to remove the optional section. The viewContext parameter of the SelectNodes method identifies the section that is to be removed, in this case CTRL10. To retrieve this parameter value, double-click the optional section control in the form view, click the Advanced tab of the Section Properties dialog box, and note the value of the ViewContext field.

• • • • • • • • •

CurrentView.ExecuteAction(ActionType.XOptionalRemove, "Description_15"); } } Select the Debug | Start Debugging menu command. If a warning message displays click OK to dismiss it. In the InfoPath preview, change the Priority drop-down list value to Low. Note: The Issue Description section appears. Change the Priority drop-down list value to None. Note: The Issue Description section disappears. Optional: Change the Priority drop-down list value to either Normal or High. In Microsoft Visual Studio 2005, select the Debug | Stop Debugging menu command. Close Microsoft Visual Studio 2005.

Exercise 2 – Using Template Parts Microsoft Office InfoPath 2007 introduces template parts as a means for developers to reuse specific form sections. For example, if an organization has multiple forms, all using the same header section, that header section can be created as a template part before being referenced in each of the forms. In this exercise, you will create a template part, insert that part into a form, and then update that part. The goal of this exercise is to learn how template parts can improve efficiency in form design. You will create a template part that will later be inserted into an existing InfoPath form template.

• Navigate to Start | Programs | Microsoft Office | Microsoft Office InfoPath 2007. • In the Getting Started dialog box, click Design a Form Template. • In the Design a Form dialog box, click Template Part in the Design a new section.

• Click Blank in the Based on section and click OK to design a new blank template part. In our scenario, Woodgrove Bank has several InfoPath forms that all contain the same header information. For this task, you will create that header and save it as a template part.

• • • • • • • • • • •

In the Design Tasks task pane, click Layout. In the Layout task pane, click the Table with Title layout table. In the form view, click the “Click to add a title” text and type Title Placeholder. Click the “Click to add form content” text and type Woodgrove Bank. Press ENTER. Optional: Set the “Woodgrove Bank” text to bold and set the spacing-after property for that text to 6pt using the Format | Paragraph menu command. In the Layout task pane, click the Custom Table layout table. In the Insert Table dialog box, type 3 in the Number of columns text box and type 2 in the Number of rows text box. Click OK. Within the first cell of the first row in the layout table, type Sales Representative:. In the Layout task pane, use the drop-down list at the top to switch to the Controls task pane.

• Drag a Text Box control from the Controls task pane to the first cell of the first row in the layout table. • Continue adding labels and controls to the layout table until the form view appears as shown in Figure 8.4. The Web Site and Employee ID fields should be text boxes, and the Region field should be a drop-down list box. Doubleclick the Region list box and manually set its entries to New York, Los Angeles, and Seattle.

Figure 8.4. Creating the Template Part

You will insert the Woodgrove Bank header template part from the previous task into an existing sample expense report form template.

• • • • • •

Select the File | Save menu command. Note: If a message dialog pops up click OK to dismiss it. In the Save As dialog box, navigate to C:\HOL\InfoPathDevelopment\Exercise2. Change the File name text box value to Woodgrove. Click Save. Close Office InfoPath 2007.

• • • • •

From Windows Explorer, navigate to C:\HOL\InfoPathDevelopment\Exercise2. Right-click ExpenseReport.xsn and click the Design menu command. In the Design Tasks task pane, click Controls. In the Controls task pane, click Add or Remove Custom Controls. In the Add or Remove Custom Controls dialog box, click Add.

• In the first page of the Add Custom Control Wizard, click Template Part and click Next. • In the next page of the Add Custom Control Wizard, click Browse. • In the Browse dialog box, navigate to C:\HOL\InfoPathDevelopment\Exercise2, click Woodgrove.xtp, and click Open. • In the Add Custom Control Wizard, click Finish. • In the last page of the Add Custom Control Wizard, click Close. • In the Add or Remove Custom Controls dialog box, click OK. • In the Controls task pane, scroll to the bottom of the Insert controls list box. • In the Custom section, click Woodgrove as shown in Figure 8.5.

Figure 8.5. Inserting the Template Part • In the form view, select the “Title Placeholder” text and type Expense Report. The form view should now appear as shown in Figure 8.6.

Figure 8.6. Modifying the Template Part in the Expense Report Form • Select the File | Save menu command. • Close Office InfoPath 2007. You will edit the Woodgrove Bank template part and then modify the expense report form to use that updated template part.

• From Windows Explorer, navigate to C:\HOL\InfoPathDevelopment\Exercise2. • Double-click Woodgrove.xtp. • Add the “Comments” text and a rich text box control to the layout table so that the form view appears as shown in Figure 8.7.

Figure 8.7. Editing the Template Part • • • •

Select the File | Save menu command. Select the File | Close menu command. In the Getting Started dialog box, click Design a Form. In the Design a Form Template dialog box, click ExpenseReport in the

• • • • • • • • • • • • • • •

Recent form templates section. In the Design Tasks task pane, click Controls. In the Controls task pane, click Add or Remove Custom Controls. In the Add or Remove Custom Controls dialog box, click Add. In the first page of the Add Custom Control Wizard, click Template Part and click Next. In the next page of the Add Custom Control Wizard, click Browse. In the Browse dialog box, navigate to C:\HOL\InfoPathDevelopment\Exercise2, click Woodgrove.xtp, and click Open. In the Add Custom Control Wizard, click Finish. In the last page of the Add Custom Control Wizard, click Close. In the Add or Remove Custom Controls dialog box, click OK. Note: The template part section control has an information icon in the topright corner. Right-click the template part section control and select the More Details menu command. In the Microsoft Office InfoPath dialog box, click Update. In the Microsoft Office InfoPath dialog box, click OK. Note: The latest version of the template part appears in the form view. Close Office InfoPath 2007 without saving changes.

Exercise 3 – Hosting an InfoPath Form as a Windows Forms Control Microsoft Office InfoPath 2007 offers developers the ability to host an InfoPath form as a native .NET control or ActiveX COM component in their applications. Thus, custom applications using Windows Forms can include an embedded InfoPath form. In this exercise, you will embed an InfoPath form in a Windows Forms application. You will also see how the Office InfoPath 2007 managed object model is available to a host Windows Forms application. You will add an InfoPath runtime .NET component to an existing Windows Forms application and then start a sample InfoPath status report form from that application.

• From Windows Explorer, navigate to C:\HOL\InfoPathDevelopment\Exercise3\SimpleWinForm. • Double-click SimpleWinForm.sln. • In the Solution Explorer right-click on Form1.cs and choose View Designer. • If the Toolbox does not appear in Microsoft Visual Studio 2005, select the View | Toolbox menu command.

The Windows Forms application already has a standard button control that will be used to start a sample InfoPath status report form. You will add a FormControl .NET component that will be used to host the InfoPath runtime. This component exposes the Office InfoPath 2007 managed object model to the Windows Forms application and allows users to enter status report data

• Right-click inside the Toolbox and select the Choose Items menu command. • In the Choose Toolbox Items dialog box, click the FormControl .NET framework component as shown in Figure 8.8.

from within that application.

Figure 8.8. Selecting the FormControl Component • Click OK. • Drag the FormControl component from the Toolbox into your form designer. • Expand the size of the FormControl component in your form designer so that it appears as shown in Figure 8.9.

Figure 8.9. Expanding the Size of the FormControl Component Now that the FormControl component has been added to the form, all that remains is a single line of code to load an InfoPath form into the component. The NewFromSolution function is used to start the form template StatusReport.xsn, located in the Exercise3 folder.

• In the form designer, double-click the button1 button control, which has a Status Report label. • Insert code snippet InfoPathDevelopment > Exercise3 > Code1LoadStatusReport or type the following code within the Click event handler for the button1 button control below the Code1-LoadStatusReport comment. private void button1_Click(object sender, EventArgs e) { // Insert InfoPathDevelopment > Exercise3 > Code1LoadStatusReport formControl1.NewFromFormTemplate(@"C:\HOL\InfoPathDevelo pment\Exercise3\StatusReport.xsn"); // Insert InfoPathDevelopment > Exercise3 > Code2EnableSetRegion

} • Select the Debug | Start Debugging menu command. • In the Windows Forms application, click Status Report. • Note: The sample InfoPath status report form loads in the FormControl component. • Optional: Explore the functionality of the status report form in the FormControl component. • Close the Windows Forms application. You will enable the Set Region button control in the Windows Forms application and change the status report Region field to a specific value.

• Insert code snippet InfoPathDevelopment > Exercise3 > Code2EnableSetRegion or type the following code within the Click event handler for the button1 button control below the Code2-EnableSetRegion comment. private void button1_Click(object sender, EventArgs e) { // Insert InfoPathDevelopment > Exercise3 > Code1-LoadStatusReport formControl1.NewFromFormTemplate(@"C:\HOL\InfoPathDevelo pment\Exercise3\StatusReport.xsn");

This code will enable the Set Region button control after the status report form loads in the FormControl component. In order to change the value of a specific field in the status report form, you will need to add code that uses the Office InfoPath 2007 managed object model. In this example, the SelectSingleNode method is used to select the Region node, and that node value is then set to Los Angeles. To save time during this task, the XmlNamespaceManager class has already been defined.

// Insert InfoPathDevelopment > Exercise3 > Code2-EnableSetRegion button2.Enabled = true; } • Insert code snippet InfoPathDevelopment > Exercise3 > Code3SetRegionValue or type the following code within the Click event handler for the button2 button control below the Code3-SetRegionValue comment. private void button2_Click(object sender, EventArgs e) { // Define the XmlNamespaceManager class XPathDocument document = new XPathDocument(@"c:\HOL\InfoPathDevelopment\Exercise3\Sta tusReportTemplate.xml"); XPathNavigator navigator = document.CreateNavigator(); XmlNamespaceManager NamespaceManager = new XmlNamespaceManager(navigator.NameTable); NamespaceManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD /2005-08-11T00:54:14"); // Insert InfoPathDevelopment > Exercise3 > Code3-

SetRegionValue formControl1.XmlForm.MainDataSource.CreateNavigator().Se lectSingleNode("//my:Region", NamespaceManager).SetValue("Los Angeles"); } • • • • • •

Select the Debug | Start Debugging menu command. In the Windows Forms application, click Status Report. Note: The Set Region button control is enabled. Click Set Region. Note: The Region field value is changed. Optional: Explore other Office InfoPath 2007 managed object model functionality in your form code.

Conclusion I.

Conclusion Microsoft Office InfoPath 2007 introduces new development features that expand previous functionality and enhance the form-design experience. The new Office InfoPath 2007 development environment and managed object model makes it easy for developers to build solutions using Visual Studio, and support for template parts in the designer allows developers to reuse sections across forms. In addition, the FormControl component provides developers the ability to host the InfoPath runtime in other custom applications, such as Windows Forms.