425-882-1032 info@3sharp.com

A long time ago I wrote an article about extending save functionality in Microsoft Office InfoPath forms. The C# code samples in that article all showed types and members of the Microsoft.Office.Interop.InfoPath.SemiTrust namespace, which is compatible with the Office InfoPath 2003 object model.

Recently, I had to develop custom save functionality for an Office InfoPath 2007 form template. Like one of the samples in the article, the form template had to assume “control” of the Save As dialog box by providing a default location and file name. So, I basically re-wrote that sample with the Microsoft.Office.InfoPath namespace. The following shows what it looks like in the Office InfoPath 2007 solution.

public void FormEvents_Save(object sender, SaveEventArgs e)
{
    string userPrincipalName = MainDataSource.CreateNavigator().SelectSingleNode("//my:userPrincipalName", NamespaceManager).InnerXml;
    string projectName = MainDataSource.CreateNavigator().SelectSingleNode("//my:project", NamespaceManager).InnerXml;
    string startDate = MainDataSource.CreateNavigator().SelectSingleNode("//my:startDate", NamespaceManager).InnerXml;
    if (this.New || e.IsSaveAs == false)
    {
        if (e.IsSaveAs)
        {
            this.SetSaveAsDialogLocation(@"c:temp");
            this.SetSaveAsDialogFilename(userPrincipalName + "_" + projectName + "_" + startDate);
        }
        e.CancelableArgs.Cancel = false;
        e.PerformSaveOperation();
    }
    else
    {
        e.CancelableArgs.Message = "This functionality has been disabled.";
        e.CancelableArgs.MessageDetails = "Only new forms can use the 'Save As' functionality.";
    }
}

NOTE: Save using custom code is not supported in browser forms.