Custom Saving in InfoPath

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.

Print | posted on Friday, December 21, 2007 10:08 AM

Feedback

# re: Custom Saving in InfoPath

Left by ehaze at 5/14/2008 7:32 AM
Gravatar thank you!

this is exactly what i was looking for!

# re: Custom Saving in InfoPath

Left by Richard at 11/3/2008 2:14 PM
Gravatar Where would you insert this code into an InfoPath form?

# re: Custom Saving in InfoPath

Left by David Gerhardt at 11/3/2008 2:19 PM
Gravatar Hi, Richard.

You need to create a Save event, which can be done by clicking "Edit" in the "Open and Save" category of the "Form Options" dialog box. Your code would then be inserted into the event handler stub.

Regards,
David
Comments have been closed on this topic.

Copyright © David Gerhardt

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski