If your form template is set to full trust security, InfoPath provides the Save() and SaveAs() methods for you to save your form via code. Although InfoPath does not provide a method for you to display the Save As dialog box to the user, it is possible to do this using managed code and the .NET Framework.
In this task, we will create a simple full trust form with a button that will display a standard Save As dialog box, and then save the form. To complete this task, you will need to have installed a copy of Visual Studio .NET, the InfoPath Toolkit for Visual Studio .NET, and the InfoPath Software Development Kit. We will use RegForm.exe to create a full trust installation package for our form template.
Create a new InfoPath managed code project:
- Launch Visual Studio.
- Choose New | Project from the File menu.
- In the Project Types tree of the New Project dialog box, expand Microsoft Office InfoPath Projects, and then select Visual C# Projects.
- In the Templates list, select InfoPath Form Template.
- Name your project SaveAsDialog (as shown in Figure 1), click OK, and then click Finish.
Figure 1: Creating a new InfoPath Visual C# managed code project.
Design the view:
- Type Full Name:, and then press Enter.
- Open the Controls task pane.
- Insert a Text Box and a Button into the view.
Set the form template to full trust security:
- Choose Form Options from the Tools menu.
- On the Security tab of the Form Options dialog box, clear the Automatically Determine Security Level check box, select Full Trust, and then click OK.
Prepare the Save As button:
- Double-click the button.
- In the Button Properties dialog box, change the Label to Save As.
- Change the ID to btnSaveAs (as shown in Figure 2).
- Click Edit Form Code.
Figure 2. The completed view.
Add a .NET component reference:
- In the Solution Explorer pane, right-click References, and then choose Add Reference (refer to Figure 3).
Figure 3: Adding a new reference.
- On the .NET tab of the Add Reference dialog box, select System.Windows.Forms.dll, click Select (as shown in Figure 4), and then click OK.
Figure 4: Adding a reference to System.Windows.Forms.dll
- At the top of the FormCode.cs file, add the following line of code:
using System.Windows.Forms; |
System.Windows.Forms includes an Application object. Adding this reference causes an ambiguity conflict in our existing code that must be fixed.
Update conflicting references to the Application object:
- Replace the variable declaration for thisApplication with the following line:
private Microsoft.Office.Interop.InfoPath.SemiTrust.Application thisApplication; |
- Replace the _Startup function declaration with the following line:
public void _Startup(Microsoft.Office.Interop.InfoPath.SemiTrust.Application app, XDocument doc) |
Add code behind the Save As button:
- Replace the contents of the btnSaveAs_OnClick function with the following code:
string sFullName = thisXDocument.DOM.selectSingleNode("/my:myFields/my:field1").text; FileDialog oDialog = new SaveFileDialog(); oDialog.DefaultExt = "xml"; oDialog.FileName = "My Form - " + sFullName; oDialog.Filter = "InfoPath Form (*.xml)|*.xml"; if(oDialog.ShowDialog() == DialogResult.OK) { string sFilename = oDialog.FileName; thisXDocument.SaveAs(sFilename); } |
Save the form template:
- Switch back to the InfoPath designer.
- Choose Publish from the File menu.
- In the Publishing Wizard dialog box, click Next.
- Select To A Shared Folder, and then click Next.
- Set the filename to C:\SaveAsDialog.xsn, and then click Next, click Finish, and then click Close.
- Exit Visual Studio.
Create a full trust installation package for the form template:
- Open a command prompt (usually available from Start | All Programs | Accessories).
- Change directories to C:\.
- Type the following command:
regform SaveAsDialog.xsn /MSI /T Yes |
- After the successful build of your install package, type the following command:
- Follow the instructions of the SaveAsDialog Setup Wizard to install your full trust form template.
Try it:
- Launch InfoPath.
- In the Fill Out A Form dialog box, double-click SaveAsDialog.
- Type your full name, and then click Save As.
- Choose a location to save your form (as shown in Figure 5), and then click Save.
Figure 5: The Save As dialog box as launched from a form button.
- Close InfoPath.
- Open Windows Explorer, locate the form you just saved, and double-click it.
The form you saved opens in InfoPath with your name filled out in the Full Name field. If you desire, there are a few other settings you can change on the Save As dialog that you display.
©2006 Greg Collins. All rights reserved. Licensed to Autonomy Systems, LLC for display on InfoPathDev.com.