Share via


How to: Access Form Data

When you want to extend the functionality of an InfoPath form, it is often necessary to programmatically access information about the form's underlying XML document, access the data that the XML document contains, or perform some action on the XML document. The InfoPath object model supports accessing and manipulating a form's underlying XML document through the use of the XmlForm class in association with the XmlFormCollection class.

The XmlForm class is one of the most useful types in the InfoPath object model because it provides a variety of properties and methods that not only interact with a form's underlying XML document, but also perform many of the actions that are available in the InfoPath user interface.

Overview of the XmlFormCollection Class

The XmlFormCollection class provides the following methods and properties that form developers can use to manage the XmlForm objects that the collection contains.

Name Description

New method

Creates a new form based on the specified form.

New method (overload 1)

Creates a new form based on the specified form using the specified open mode behavior.

NewFromFormTemplate method

Creates a new form based on the specified form template.

NewFromFormTemplate method (overload 1)

Creates a new form based on the specified form template and XML data.

NewFromFormTemplate method (overload 2)

Creates a new form based on the specified form template with data specified by an XPathNavigator object.

NewFromFormTemplate method (overload 3)

Creates a new form based on the specified form template with data specified by an XPathNavigator object using the specified open mode behavior.

Open method

Opens the specified form.

Open method (overload 1)

Opens the specified form using the specified open mode behavior.

Count property

Gets a count of the number of XmlForm objects contained in the collection.

Item property

Gets a reference to the specified XmlForm object from the collection by index value.

Overview of the XmlForm Interface

The XmlForm class provides the following methods and properties, which form developers can use to interact with and perform actions on a form's underlying XML document.

Name Description

Close method

Closes the form.

GetWorkflowTasks method

Gets a reference to a Microsoft.Office.Core.WorkflowTasks collection for the current form.

GetWorkflowTemplates method

Gets a reference to a Microsoft.Office.Core.WorkflowTemplates collection for the current form.

MergeForm method

Merges the current form with the form specified by path or URL.

MergeForm method (overload 1)

Merges the current form with the target form specified in the node returned by the XPathNavigator passed to the method.

NotifyHost method

Provides a custom value to the hosting application or ASPX page.

Print method

Prints the form content as it is rendered in the form's active view.

Print method (overload 1)

Prints the form content as it is rendered in the form's active view by displaying the Print dialog box.

Save method

Saves the form to the Uniform Resource Locator (URL) that it is currently associated with.

SaveAs method

Saves the form to the specified Uniform Resource Locator (URL).

SetSaveAsDialogFilename method

Sets the default filename for the SaveAs dialog box.

SetSaveAsDialogLocation method

Sets the default path for saving the form using the SaveAs dialog box.

Submit method

Submits the form using the submit operation defined in the form template.

CurrentView property

Gets a View object that represents the current view of the form.

DataConnections property

Gets a DataConnectionCollection object associated with the form.

DataSources property

Gets the DataSourceCollection object associated with the form.

Dirty property

Gets a value that indicates whether the data in a form has been modified since it was last saved.

Errors property

Gets a reference to the FormErrorCollection that is associated with a form.

Extension property

Gets an Object for accessing the functions and global variables contained in a form's primary form code file using System.Reflection.

FormState property

Gets a reference to a property bag of type System.Collections.IDictionary that browser-enabled forms can use to maintain state information across sessions on the server.

Host property

Gets a System.Object that code running in a hosted instance of InfoPath can use to access the object model of the host application.

Hosted property

Gets whether InfoPath is hosted as a control in another application.

HostName property

Gets the name of the application hosting InfoPath as a control.

MainDataSource property

Gets a DataSource object that represents the main data source of the form.

NamespaceManager property

Gets a reference to a XmlNamespaceManager object that can be used to resolve, add, or remove namespaces used in the form.

New property

Gets a value that specifies whether a form is new.

Permission property

Gets a reference to a Permission object associated with the form.

QueryDataConnection property

Gets a reference to the DataConnection object that represents the data connection that is associated with the form.

ReadOnly property

Gets a value that indicates whether a form template is read-only or locked.

Recovered property

Gets a value that indicates whether a form was last saved by an AutoRecover save operation.

Signed property

Gets a value that indicates whether a form has been digitally signed using digital signatures.

SignedDataBlocks property

Gets a reference to the SignedDataBlockCollection collection that is associated with a form.

TaskPanes property

Gets a reference to the TaskPaneCollection that is associated with a form template.

Template property

Gets a reference to the FormTemplate object that represents the manifest (.xsf) of the form template associated with the form.

Uri property

Gets the Uniform Resource Identifier (URI) of a form.

UserRole property

Gets or sets the current user of the form's role name.

ViewInfos property

Gets a reference to the ViewInfoCollection object associated with the form template.

XmlLang property

Gets the value of the xml:lang attribute in the underlying XML document of the form.

Using the XmlFormCollection Class

The XmlFormCollection class is accessed through the XmlForms property of the Application class. In a managed code form template created using the object model provided by the members of the Microsoft.Office.InfoPath namespace, you can use the this (C#) or Me (Visual Basic) keyword in your form code to access the Application class and its members.

The following example uses the XmlForms property of the Application class to create an object variable named myForms that references the XDocumentsCollection object of the currently running instance of InfoPath. This variable is then used to display the count of forms that are open.

// Create variable for accessing the XmlFormCollection.
XmlFormCollection myForms = this.Application.XmlForms;

// Display the number of forms that are currently open.
MessageBox.Show("Forms open: " + myForms.Count);
// Create variable for accessing the XmlFormCollection.
Dim myForms As XmlFormCollection = Me.Application.XmlForms

' Display the number of forms that are currently open.
MessageBox.Show("Forms open: " + myForms.Count)

The myForms variable can then also be used to create new forms (using one of the New or NewFromTemplate methods) or open existing forms (using one of the Open methods).

Using the XmlForm Class

In a managed code form template created using the object model provided by the members of the Microsoft.Office.InfoPath namespace, you can use the this (C#) or Me (Visual Basic) keyword in your form code to access the members of the XmlForm class directly (without requiring a reference to the XmlForm keyword).

Accessing a Form's Property Values

The following example uses the this or Me keyword to access the New, ReadOnly, Signed, and Uri properties of the XmlForm class and display the values returned for the current form in a message box.

MessageBox.Show(
   "Is new: " + this.New + System.Environment.NewLine +
   "Is read-only: " + this.ReadOnly + System.Environment.NewLine +
   "Is signed: " + this.Signed + System.Environment.NewLine +
   "Form URI: " + this.Uri);
MessageBox.Show( _
   "Is new: " & this.New + System.Environment.NewLine & _
   "Is read-only: " & this.ReadOnly & System.Environment.NewLine + _
   "Is signed: " & this.Signed & System.Environment.NewLine & _
   "Form URI: " & this.Uri)

Accessing a Form's Data Source

A key property of the XmlForm class with respect to form data is the MainDataSource property. This property returns a reference to a DataSource object that represents the underlying XML data of the current form, which is also referred to as the form's main or primary data source. The DataSource class provides the CreateNavigator method, which creates a System.Xml.XPath.XPathNavigator object positioned at the root of the form's underlying XML document. The properties and methods of the XPathNavigator class can then be used to navigate and edit the form's underlying XML data.

The following example uses the MainDataSource property of the XmlForm class to create an XPathNavigator object positioned at the root of the form's main data source. The OuterXml property of the XPathNavigator class is then used to return and display all of the contents of a form's underlying XML document.

// Get outer XML of the underlying XML document.
string myDoc = this.MainDataSource.CreateNavigator.OuterXml.ToString();
// Display XML.
MessageBox.Show(myDoc);
' Get outer XML of the underlying XML document.
Dim myDoc As String myDoc = _
   Me.MainDataSource.CreateNavigator.OuterXml.ToString()
' Display XML.
MessageBox.Show(myDoc)

Note

Because InfoPath treats the MainDataSource property as a default property of the XmlForm object accessed when using the this or Me keywords, you can omit it from the line of code used to create the XPathNavigator object.

To learn more about the XPathNavigator class in an InfoPath form template's business logic, see How to: Work with the XPathNavigator and XPathNodeIterator Classes.

Accessing Data About a Form's Form Template File

Information about the form template associated with a form, including the form definition file (.xsf ) and the source XML data that it contains, can also be accessed using the XmlForm class. This information is accessed using the Template property, which returns a reference to a FormTemplate object that represents the form template associated with the current form.

In the following example, the first message box displays some of the data that is available through the Template class, such as its Uniform Resource Identifier (URI) location (using the Uri property), the cache identifier (using the CacheId property) and its version number (using the Version property). The next message box uses the Manifest property of the Template class to create an XPathNavigator object that is used to display the source XML of the form definition file (.xsf).

// Display form template properties.
MessageBox.Show(
   "Cache ID: " + this.Template.CacheId + System.Environment.NewLine +
   "URI: " + this.ReadOnly + System.Environment.NewLine +
   "Version: " + this.Signed, "Form Template Properties");

// Display form definition file XML.
MessageBox.Show(this.Template.Manifest.OuterXml, 
   "Form Definition File XML");
' Display form template properties.
MessageBox.Show( _
   "Cache ID: " & Me.Template.CacheId & System.Environment.NewLine &
   "URI: " & Me.ReadOnly & System.Environment.NewLine &
   "Version: " & Me.Signed, "Form Template Properties")

' Display form definition file XML.
MessageBox.Show(Me.Template.Manifest.OuterXml, _
   "Form Definition File XML")