Export (0) Print
Expand All

Initialization and Clean-up Code Using InfoPath 2003 Object Model

Office 2013

Last modified: June 28, 2011

Applies to: InfoPath 2013 | InfoPath Forms Services | Office 2013 | SharePoint Server 2013

In this article
The _Startup Method
The _ShutDown Method
Initialization and Clean-up Code Example

By default, the FormCode.cs or FormCode.vb file that is created for a form template project that is compatible with InfoPath 2003 contains all the source code for the programming logic of the form. The template for the project generates a class in the FormCode.cs or FormCode.vb file much like the classes in the following examples where you can define initialization and clean-up code, as well as handlers for form events. The FormCode.cs and FormCode.vb files apply an assembly-level System.ComponentModel.DescriptionAttribute attribute, which identifies the class as the only class where event handlers are implemented. The InfoPathNamespace attribute (which is implemented by the InfoPathNamespaceAttribute type) is applied to a class to identify the XML DOM selection namespaces used within the class. The namespaces referenced in the InfoPathNamespace are maintained by the InfoPath project system.

The FormCode class itself provides _Startup and _Shutdown methods that are used to perform initialization and clean-up routines for any components that are required in addition to standard InfoPath functionality while the form is open.

Important note Important

Do not call members of the InfoPath object model from within the _Startup and _Shutdown methods. You should initialize and call only members of external components in these methods.

using System;
using Microsoft.Office.Interop.InfoPath.SemiTrust;

// Office integration attribute. Identifies the startup class for the form. Do not
// modify.
[assembly: System.ComponentModel.DescriptionAttribute(
    "InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")]

namespace Template1
{
    // The namespace prefixes defined in this attribute must remain synchronized with
    // those in the form definition file (.xsf).
    [InfoPathNamespace(
        "xmlns:my='http://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-29T22-27-27'")]
    public partial class FormCode
    {
        private XDocument thisXDocument;
        private Application thisApplication;

        public void _Startup(Application app, XDocument doc)
        {
            thisXDocument = doc;
            thisApplication = app;

            // You can add additional initialization code here.
        }

        public void _Shutdown()
        {
        }
    }
}

In addition to providing a place to write initialization code for additional components, the _Startup method initializes the thisXDocument and thisApplication variables that can be used in your form code to access the members of the XDocument and Application classes in the InfoPath object model. The code necessary to initialize the two variables is generated automatically by the project template.

    private XDocument thisXDocument;
    private Application thisApplication;

    public void _Startup(Application app, XDocument doc)
    {
        thisXDocument = doc;
        thisApplication = app;

        // You can add additional initialization code here.
    }

The following examples show a simple event handler for a button that uses the thisXDocument variable to access the Alert method of the UIObject type.

[InfoPathEventHandler(MatchPath="CTRL1_5", EventType=InfoPathEventType.OnClick)]
public void CTRL1_5_OnClick(DocActionEvent e)
{
    // Write your code here.
    thisXDocument.UI.Alert("Hello!");
}

For information on how to create an event handler, see How to: Add an Event Handler Using the InfoPath 2003 Object Model.

The _Shutdown method is the last method called when a form is closed. You can write any code in this method that is needed to clean up or finalize components used in the form.

    public void _Shutdown()
    {
    }

The following example shows how to initialize a connection to a Microsoft SQL Server database in the _Startup method and close the connection in the _Shutdown method. In order for this example to work correctly, you must first set a reference to the System.Data assembly of the .NET Framework by clicking Add Reference on the Project menu, and then selecting the System.Data.dll component on the .NET tab. Also note that the using System.Data.SqlClient (or Imports System.Data.SqlClient) directive was added at the top of the form code file to reduce keystrokes.

Note Note

Users of an InfoPath form that contains form code that connects to a SQL Server database may require security permissions depending on how the form is deployed and security policy is defined. For more information on security see About the Security Model for Form Templates with Code and How to: Configure Security Settings for Form Templates with Code.

using System;
using System.Data.SqlClient;
using Microsoft.Office.Interop.InfoPath.SemiTrust;

// Office integration attribute. Identifies the startup class for the form. Do not
// modify.
[assembly: System.ComponentModel.DescriptionAttribute(
    "InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")]

namespace Template1
{
    // The namespace prefixes defined in this attribute must remain synchronized with
    // those in the form definition file (.xsf).
    [InfoPathNamespace(
        "xmlns:my='http://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-05T20-56-13'")]
    public partial class Template1
    {
        private XDocument    thisXDocument;
        private Application    thisApplication;
        private SqlConnection sqlConnect;

        public void _Startup(Application app, XDocument doc)
        {
            thisXDocument = doc;
            thisApplication = app;

            // Initialize variable for SQL Server connection.
            sqlConnect= new SqlConnection("server=localhost;Trusted_Connection=yes;database=Northwind");
        }

        public void _Shutdown()
        {
            // Close SQL Server connection at shut down.
            sqlConnect.Close();
        }
    }
}
Show:
© 2014 Microsoft