Display a Save As Dialog Box - Greg Collins

InfoPath Dev

This Blog


Greg Collins

Display a Save As Dialog Box

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:

  1. Launch Visual Studio.
  2. Choose New | Project from the File menu.
  3. In the Project Types tree of the New Project dialog box, expand Microsoft Office InfoPath Projects, and then select Visual C# Projects.
  4. In the Templates list, select InfoPath Form Template.
  5. 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:

  1. Type Full Name:, and then press Enter.
  2. Open the Controls task pane.
  3. Insert a Text Box and a Button into the view.

Set the form template to full trust security:

  1. Choose Form Options from the Tools menu.
  2. 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:

  1. Double-click the button.
  2. In the Button Properties dialog box, change the Label to Save As.
  3. Change the ID to btnSaveAs (as shown in Figure 2).
  4. Click Edit Form Code.

Figure 2. The completed view.

Add a .NET component reference:

  1. In the Solution Explorer pane, right-click References, and then choose Add Reference (refer to Figure 3).

Figure 3: Adding a new reference.

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

  1. 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:

  1. Replace the variable declaration for thisApplication with the following line:

private Microsoft.Office.Interop.InfoPath.SemiTrust.Application thisApplication;

  1. 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:

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

Save the form template:

  1. Switch back to the InfoPath designer.
  2. Choose Publish from the File menu.
  3. In the Publishing Wizard dialog box, click Next.
  4. Select To A Shared Folder, and then click Next.
  5. Set the filename to C:\SaveAsDialog.xsn, and then click Next, click Finish, and then click Close.
  6. Exit Visual Studio.

Create a full trust installation package for the form template:

  1. Open a command prompt (usually available from Start | All Programs | Accessories).
  2. Change directories to C:\.
  3. Type the following command:

regform SaveAsDialog.xsn /MSI /T Yes

  1. After the successful build of your install package, type the following command:


  1. Follow the instructions of the SaveAsDialog Setup Wizard to install your full trust form template.

Try it:

  1. Launch InfoPath.
  2. In the Fill Out A Form dialog box, double-click SaveAsDialog.
  3. Type your full name, and then click Save As.
  4. 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.

  1. Close InfoPath.
  2. 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.



Karsten Brocksieper said:

Hi Greg,

thank you very much for your article and the advice using "System.Windows.Forms" to show a file dialog.

Unfortunately this doesn´t work if you want to show your form in a browser instead of the rich client.

Do you have any tips how I can accomplish this in a browser?

Thank you,


November 15, 2007 9:38 AM

Greg Collins said:

Sorry, I do not believe there is a workaround for the browser. Browser forms have numerous limitations.

January 15, 2008 10:01 AM

Ejuozaitis said:

I see that this would save in C drive but what if you have the form on Sharepoint and you want to change the filename when the user saves the file?  Will the coding be the same as above or would there be some changes?

February 6, 2009 7:25 AM
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.