Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to programmatically change the default view of an InfoPath form


View products that this article applies to.

Summary

Microsoft Office InfoPath forms support multiple views. One view is the default view. When you open a form in Microsoft InfoPath 2010, Microsoft Office InfoPath 2007, or Microsoft Office InfoPath 2003, you open the form in the default view. This article contains an example of how to programmatically change the default view of an InfoPath form.

Create a New Form

  1. Start InfoPath.
    • For InfoPath Designer 2010:
      1. Click New, click Blank Form, and then click Design Form.
    • For InfoPath 2007:
      1. Click Design a Form Template on the File menu.
      2. Click Blank in the Design a Form Template task pane, and then click OK.
    • For InfoPath 2003:
      1. Click Design a Form on the File menu.
      2. Click New Blank Form in the Design a Form task pane.

Create View 1

For InfoPath 2010:
  1. On the blank form, type This is View 1 for the title, and then type Check to display View 2 when the form is opened under the dotted line.
  2. Click to position your pointer at the start of the second line.
  3. In Controls, double-click Check Box.
  4. Click the Save icon.
For InfoPath 2007 and InfoPath 2003:
  1. On the blank form, type This is View 1 on the first line, and then type Check to display View 2 when the form is opened on the second line.
  2. Click to position your pointer at the start of the second line.
  3. On the Task Pane drop-down menu, click Controls.
  4. In the Controls task pane, click Check Box.
  5. Click the Save icon.

Create View 2

For InfoPath 2010:
  1. Click the Page Design tab. Notice that View 1 is the default view for the form.
  2. In the Views section, click New View.
  3. Type the name View 2, and then click OK.
  4. For the title, type This is View 2.
For InfoPath 2007 and InfoPath 2003:
  1. On the Task Pane drop-down menu, click Views.

    View 1 is the default view for the form.
  2. In Actions section of the task pane, click Add a New View.
  3. Type the name View 2, and then click OK.
  4. On the blank form, type This is View 2.

Add the Script

InfoPath 2007

In InfoPath 2010 and 2007, you must set the programming language that you want to use. For InfoPath 2003, skip this procedure and then use the Add the script procedure.

Note This example uses the C# programming language. You can use Visual Basic or other available options.
Set the programming language
For InfoPath 2010:

Before you run this sample script in InfoPath 2010, you must change the programming language to C#. To do this, follow these steps.

Warning The Remove Code command that is mentioned in the following steps removes all existing code from the current form. Therefore, do not use this option unless you are sure that you want to perform this action.
  1. Click the Developer tab.
  2. In the Code section, click Language, and then click to select Programming. If the Form template code language box is disabled, click Remove Code to remove all existing code in the form. If the Form template code language box is available, go to the next step.
  3. In the Form template code language box, click to select C#, and then click OK.
For InfoPath 2007:

Before you run this sample script in InfoPath 2007, you must change the programming language to C#. To do this, follow these steps:

Warning The Remove Code command that is mentioned in the following steps removes all existing code from the current form. Therefore, do not use this option, unless you are sure that you want to perform this action.
  1. On the Tools menu, click Form Options.
  2. In the Category list, click Programming. If the Form template code language box is disabled, click Remove Code to remove all existing code in the form. If the Form template code language box is available, go to the next step.
  3. In the Form template code language box, click C#, and then click OK.
Add the script
For InfoPath 2010:

Add the script to the Loading Event. To do this, follow these steps:
  1. On the Developer tab, click Loading Event. Microsoft Visual Studio Tools for Applications starts.
  2. Add the following code example in the locations that are indicated.
    {
    //Create an XPathNavigator object for the main DOM
    XPathNavigator xnDocument = this.MainDataSource.CreateNavigator();

    //Create an XPathNavigator object for field1 - the check box for switching the View
    XPathNavigator xnDefaultView = xnDocument.SelectSingleNode("/my:myFields/my:field1", this.NamespaceManager);

    //Make sure that the XPathNavigator object is not null or an empty string
    if ((xnDefaultView != null) && (xnDefaultView.Value != ""))

    {
    //Switch to the appropriate View
    switch (xnDefaultView.Value)

    {
    case "false":
    e.SetDefaultView("View 1");
    break;

    case "true":
    e.SetDefaultView("View 2");
    break;

    }
    }
    }
  3. Save the changes, and then close Visual Studio Tools for Applications.
  4. Save your form template as SwitchDefaultView.xsn, and then close the form template.
For InfoPath 2007

Add the script to the Loading Event. To do this, follow these steps:
  1. On the Tools menu, point to Programming, and then click Loading Event. Microsoft Visual Studio Tools for Applications starts.
  2. Add the following code example to the Age_OnValidate event.
    {
    //Create an XPathNavigator object for the main DOM
    XPathNavigator xnDocument = this.MainDataSource.CreateNavigator();

    //Create an XPathNavigator object for field1 - the check box for switching the View
    XPathNavigator xnDefaultView = xnDocument.SelectSingleNode("/my:myFields/my:field1", this.NamespaceManager);

    //Make sure that the XPathNavigator object is not null or an empty string
    if ((xnDefaultView != null) && (xnDefaultView.Value != ""))

    {
    //Switch to the appropriate View
    switch (xnDefaultView.Value)

    {
    case "false":
    e.SetDefaultView("View 1");
    break;

    case "true":
    e.SetDefaultView("View 2");
    break;

    }
  3. Save the changes, and then close Visual Studio Tools for Applications.
  4. Save your form template as SwitchDefaultView.xsn, and then close the form template.

InfoPath 2003

To add the script in InfoPath 2003, follow these steps:
  1. On the Tools menu, point to Script, and then click Microsoft Script Editor.
  2. Add the following code to the Code window:
    function XDocument::OnLoad(eventObj)
    {
    //Pick the default view based on the value of element DefaultView
    var objDefaultView = XDocument.DOM.selectSingleNode("//my:field1");

    if ((objDefaultView != null) && (objDefaultView.text != ""))
    {
    switch (objDefaultView.text)
    {
    case "false":
    XDocument.ViewInfos("View 1").IsDefault = true;
    break;
    case "true":
    XDocument.ViewInfos("View 2").IsDefault = true;
    break;
    }
    }
    }
  3. Save the script, and then close the Microsoft Script Editor.
  4. Save your form template as SwitchDefaultView.xsn, and then close the form template.

Test the Script

For InfoPath 2010
  1. Open Microsoft Office InfoPath Filler.
  2. Click Find a Form, browse to SwitchDefaultView.xsn, select it, and then click Open.


    The form opens in View 1 (the initial default view).
  3. Click to select the View 2 check box.
  4. Save the form as SwitchDefaultViewForm.xml, and then close the form.
  5. On the File tab, click Close, and then click Open.
  6. Locate SwitchDefaultViewForm.xml, and then click Open.
The form opens in View 2.

For InfoPath 2007 and InfoPath 2003:
  1. On the File menu, click Fill Out a Form.

    Note Make sure that you fill out the form in this step instead of previewing the form. Preview does not reflect any programmatic change of the default view in the OnLoad event. Preview mode always shows the view that is currently selected in the InfoPath Designer.
  2. In InfoPath 2007, click SwitchDefaultView under Open a Form.

    In InfoPath 2003, click SwitchDefaultView in the task pane.

    The form opens in View 1 (the initial default view).
  3. Click to select the View 2 check box.
  4. Save the form as SwitchDefaultViewForm.xml, and then close the form.
  5. On the File menu, click Open.
  6. Locate SwitchDefaultViewForm.xml, and then click Open.
The form opens in View 2.

↑ Back to the top


Keywords: kboffice12yes, kbfreshness2006, kbsweptinfopath2003sp1, kbsweptsoltax, kbhowto, kb

↑ Back to the top

Article Info
Article ID : 826997
Revision : 3
Created on : 4/17/2018
Published on : 4/18/2018
Exists online : False
Views : 378