Launch a Supporting Form - Greg Collins
in

InfoPath Dev

This Blog

Syndication

Greg Collins

Launch a Supporting Form

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:

  1. Type Supporting Form at the top of the view, and then press Enter.
  2. From the Controls task pane, insert a text box.
  3. Choose Save from the File menu, and then click Save.
  4. Create a folder named MultiForm.
  5. Name your file Supporting.xsn, and then click Save.
  6. Close InfoPath.

Create a test document using the supporting form:

  1. Launch your Supporting.xsn file from the MultiForm folder.
  2. Type Test Data into the text box.
  3. Save your form as Form1.xml in the MultiForm folder.
  4. 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:

  1. Type Source Form at the top of the view, and then press Enter.
  2. From the Controls task pane, insert three buttons.

Identify the absolute path:

  1. Choose Programming | On Load Event from the Tools menu.
  2. Add the sFormPath variable declaration just above and outside of the OnLoad event handler so that it is globally accessible to the other functions:

var sFormPath = "";

  1. 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);

  1. Switch back to the InfoPath designer.

Add code behind the New button:

  1. Double-click the first button, change the label to New, change the ID to btnNew, and then click Edit Form Code.
  2. Add the following code to the btnNew OnClick event handler:

Application.XDocuments.New(sFormPath + "Form1.xml");

  1. Switch back to the InfoPath designer.

Add code behind the NewFromSolution button:

  1. Double-click the second button, change the label to NewFromSolution, change the ID to btnNewFromSolution, and then click Edit Form Code.
  2. Add the following code to the btnNewFromSolution OnClick event handler:

Application.XDocuments.NewFromSolution(sFormPath + "Supporting.xsn");

  1. Switch back to the InfoPath designer.

Add code behind the NewFromSolutionWithData button:

  1. Double-click the third button, change the label to NewFromSolutionWithData, change the ID to btnNewFromSolutionWithData, and then click Edit Form Code.
  2. Add the following code to the btnNewFromSolutionWithData OnClick event handler:

Application.XDocuments.NewFromSolutionWithData(sFormPath + "Form1.xml", sFormPath + "Supporting.xsn");

Save the form:

  1. Save your code and close the script editor.
  2. In the InfoPath designer, choose Save from the File menu, and then click Save.
  3. Navigate to the MultiForm folder.
  4. Name your file Source.xsn, and then click Save.
  5. 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.

Comments

 

rnichols said:

I cant get this to work, I think It is because I an using IP 2007.  What do I need to do differently?

January 9, 2008 8:58 AM
 

Greg Collins said:

This article was written for InfoPath 2003. I have not tried this in InfoPath 2007. If you are using InfoPath 2003 compatibility mode, then it should work as is, but if you are using the updated IP07 coding, then a rewrite is in order.

January 15, 2008 9:50 AM
 

cappsie said:

Is there any chance you could provide a rewrite on this as this is precisely what I require :)

Thanks,

Adam.

March 5, 2012 6:27 AM
 

makshah said:

Can you please provide rewrite perfectly in infopath 2010 or 2007

Thank You

Mak

June 22, 2012 11:51 AM
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.