InfoPath forms can do a lot of things. But at times you might want one form to launch another form to perform a different, but related task. You might have needs for numerous forms to work together as part of an overall solution.
In this task we will create several buttons which will launch a supporting form using the New, NewFromSolution, and NewFromSolutionWithData methods of the XDocuments collection. Each method requires an absolute path to the supporting form or document. Since our support form and source form as well as any documents created will all be stored in the same folder, we will grab the path from the XDocument.Solution.URI property. This will allow us to relocate our forms and templates and not need to update the code. Let's start by designing a new blank supporting form.
THE SUPPORTING FORM
Create the supporting form:
- Type Supporting Form at the top of the view, and then press Enter.
- From the Controls task pane, insert a text box.
- Choose Save from the File menu, and then click Save.
- Create a folder named MultiForm.
- Name your file Supporting.xsn, and then click Save.
- Close InfoPath.
Create a test document using the supporting form:
- Launch your Supporting.xsn file from the MultiForm folder.
- Type Test Data into the text box.
- Save your form as Form1.xml in the MultiForm folder.
- Close InfoPath.
Now that we have created our supporting form and a test document, lets create the a source form which will be used to launch the supporting form. Again, let's start by designing a new blank form.
THE SOURCE FORM
Create the source form:
- Type Source Form at the top of the view, and then press Enter.
- From the Controls task pane, insert three buttons.
Identify the absolute path:
- Choose Programming | On Load Event from the Tools menu.
- Add the sFormPath variable declaration just above and outside of the OnLoad event handler so that it is globally accessible to the other functions:
- Add the following code to the OnLoad event handler:
var sUri = XDocument.Solution.URI; var i = sUri.lastIndexOf("\\"); if(-1 == i) i = sUri.lastIndexOf("/"); sFormPath = sUri.substring(0, i + 1); |
- Switch back to the InfoPath designer.
Add code behind the New button:
- Double-click the first button, change the label to New, change the ID to btnNew, and then click Edit Form Code.
- Add the following code to the btnNew OnClick event handler:
Application.XDocuments.New(sFormPath + "Form1.xml"); |
- Switch back to the InfoPath designer.
Add code behind the NewFromSolution button:
- Double-click the second button, change the label to NewFromSolution, change the ID to btnNewFromSolution, and then click Edit Form Code.
- Add the following code to the btnNewFromSolution OnClick event handler:
Application.XDocuments.NewFromSolution(sFormPath + "Supporting.xsn"); |
- Switch back to the InfoPath designer.
Add code behind the NewFromSolutionWithData button:
- Double-click the third button, change the label to NewFromSolutionWithData, change the ID to btnNewFromSolutionWithData, and then click Edit Form Code.
- Add the following code to the btnNewFromSolutionWithData OnClick event handler:
Application.XDocuments.NewFromSolutionWithData(sFormPath + "Form1.xml", sFormPath + "Supporting.xsn"); |
Save the form:
- Save your code and close the script editor.
- In the InfoPath designer, choose Save from the File menu, and then click Save.
- Navigate to the MultiForm folder.
- Name your file Source.xsn, and then click Save.
- Close InfoPath.
Try it:
Now that we have created the source and supporting templates and a supporting test document, let's try out the various methods. Preview the form and click each of the buttons to see what happens.
One thing you'll probably notice is that when you click either the New or NewFromSolutionWithData button for the first time you will be presented with a security warning dialog box as shown in Figure 1. To learn of ways to prevent or disable this dialog box, refer to Prevent the Internet Explorer Cross-Domain Security Dialog Box.

Figure 1. The Internet Explorer cross-domain data access security risk dialog box.
©2005 Greg Collins. All rights reserved. Licensed to Autonomy Systems, LLC for display on InfoPathDev.com.