Custom task panes, and their ability to interface with objects and functions in InfoPath, can be very useful. One such example of utilizing this functionality is creating a custom task pane using some simple JavaScript code that dynamically generates links to the different views in a form. The main difference between changing views in the task pane and changing views in InfoPath is that the task pane requires code to change views, whereas you can use rules or simply the View menu items in InfoPath.
In this task we will create a custom task pane HTML file in a text editor and add it to an InfoPath sample form.
Create a custom task pane:
Copy the following code into a text editor, and then save the file as SwitchViewsTaskPane.htm.
<html> <head> <script language="JavaScript"> var XDocument; var View; function PrintLinks() { // Get a reference to the XDocument object for the InfoPath Form. XDocument = window.external.Window.XDocument;
// Set the reference to the View object. View = XDocument.View;
// Get the ViewInfos object. var oViewInfos = XDocument.ViewInfos;
var sInnerHtml = "";
// Loop through the views, generate the HTML, and store it in sInnerHtml. for(i = 0; i < oViewInfos.Count; i++) { sInnerHtml = sInnerHtml + "<a href='#' OnClick=\"View.SwitchView('" + oViewInfos(i).Name + "');\">" + (i+1) + ". " + oViewInfos(i).Name + "</a><br />" ; }
// Display the HTML that was just generated. ViewLinks.innerHTML = sInnerHtml; } </script> </head> <body onload="PrintLinks()"> <div id="ViewLinks"></div> </body> </html> |
Customize the Project Plan sample form:
- Open InfoPath, click Design A Form in the Fill Out A Form dialog box, and then click Customize A Sample in the Design A Form task pane.
- Select Project Plan from the Samples tab, and then click OK.
- Choose Form Options from the Tools menu.
- Switch to the Advanced Tab, select Enable Custom Task Pane, and then click Resource Files.
- Click Add, locate the SwitchViewsTaskPane.htm file that was created above, and then click OK.
- Name the task pane Switch Views (as shown in Figure 1), and then click OK to close the Form Options dialog box.

Figure 1. Adding the custom task pane.
Preview the form and notice that the task pane automatically generates links to all of the available views within the form, as shown in Figure 2.

Figure 2. The completed task pane applied to the Project Plan sample form.