How to: Access Application Data Using the InfoPath 2003 Object Model
The InfoPath 2003-compatible object model provides objects and collections that can be used to gain access to information about the InfoPath application, including information related to a form's underlying XML document and the form definition (.xsf) file. This data is accessed through the top-level object in the InfoPath object model hierarchy, which is instantiated by using the Application interface.
An InfoPath 2003-compatible managed code form template project created using Microsoft Visual Studio Tools for Applications (VSTA), Visual Studio 2005 with Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System, or Visual Studio 2008 with Visual Studio Tools for Office initializes the thisApplication
variable in the_Startup
method of the form code class, which can be used to access the members of the Application interface. In the following example, the Name and Version properties of the Application interface are used to return the name and version number of the running instance of InfoPath. This information is displayed in a message box by using the Alert method of the UI2 interface.
thisXDocument.UI.Alert("Application name: " + thisApplication.Name +
"\nApplication version: " + thisApplication.Version);
thisXDocument.UI.Alert("Application name: " & thisApplication.Name & _
vbNewLine & "Application version: " & thisApplication.Version)
In the Visual C# example, the reference to the \n
character in the string for the alert message is rendered by InfoPath unmanaged code as a standard new line feed that causes the text to break and be placed on a new line in the message box. To explicitly use the new line value defined for the current environment and platform, use the Environment.NewLine property instead, as shown in the following example.
thisXDocument.UI.Alert("Application name: " + thisApplication.Name +
Environment.NewLine + "Application version: " +
thisApplication.Version);
Accessing Data from the Underlying XML Document of a Form
Developers can use the XDocument interface to gain access to information about a form's underlying XML document, including a reference to an XML Document Object Model (DOM) that contains the source XML data of the form.
In the following example, the first message box displays some of the data that is available from the XDocument interface, such as whether the underlying XML document has been changed (by using the IsDirty property) and whether it has been digitally signed (using the IsSigned property). The next message box uses the XDocument interface's DOM property to display the source XML of the form's underlying XML document.
thisXDocument.UI.Alert("\nIsDirty: " + thisXDocument.IsDirty +
"\nIsDOMReadOnly: " + thisXDocument.IsDOMReadOnly +
"\nIsNew: " + thisXDocument.IsNew +
"\nIsReadOnly: " + thisXDocument.IsReadOnly +
"\nIsSigned: " + thisXDocument.IsSigned);
thisXDocument.UI.Alert(thisXDocument.DOM.xml);
thisXDocument.UI.Alert("IsDirty: " & thisXDocument.IsDirty & vbNewLine & _
"IsDOMReadOnly: " & thisXDocument.IsDOMReadOnly & vbNewLine & _
"IsNew: " & thisXDocument.IsNew & vbNewLine & _
"IsReadOnly: " & thisXDocument.IsReadOnly & vbNewLine & _
"IsSigned: " & thisXDocument.IsSigned)
thisXDocument.UI.Alert(thisXDocument.DOM.xml)
The xml property used in the previous example is a property of the XML Document Object Model (DOM). For more information about the XML DOM, see the MSXML 5.0 SDK documentation.
Accessing Data from a Form's Form Definition File
Information about a form's .xsf file, including an XML DOM reference to the source XML data that it contains, can also be accessed using the XDocument interface. This information is accessed using the Solution property, which returns a reference to the SolutionObject interface.
In the following example, the first alert displays some of the data that is available through the SolutionObject interface, such as its Uniform Resource Identifier (URI) location (using the URI property) and its version number (using the Version property). The next alert uses the DOM property of the SolutionObject interface to display the source XML of the .xsf file.
thisXDocument.UI.Alert("PackageURL: " +
thisXDocument.Solution.PackageURL +
"\nURI: " + thisXDocument.Solution.URI +
"\nVersion: " + thisXDocument.Solution.Version);
thisXDocument.UI.Alert(thisXDocument.Solution.DOM.xml);
thisXDocument.UI.Alert("PackageURL: " & _
thisXDocument.Solution.PackageURL & vbNewLine & _
"URI: " & thisXDocument.Solution.URI & vbNewLine & _
"Version: " & thisXDocument.Solution.Version)
thisXDocument.UI.Alert(thisXDocument.Solution.DOM.xml)