Working with Data in a Form's Secondary Data Sources

When working with a Microsoft Office InfoPath 2007 form, you can write scripting code to access the form's secondary data sources, which are typically external to the primary data source of a form, and then manipulate the data that they contain. The InfoPath object model supports access to a form's external data sources through the use of the DataObject object in association with the DataObjects collection.

The InfoPath object model also provides a set of data adapter objects, containing information about the secondary data sources, and access to the data that they contain. The type of data adapter that is returned by the DataObject object depends on the type of data source that was selected when the secondary data source was created in design mode.

Overview of the DataObjects collection

The DataObjects collection provides the following properties that form developers can use to manage the DataObject objects that it contains.

Name Description
Count property Returns a count of the number of DataObject objects contained in the collection
Item property Returns a reference to the specified DataObject object

Overview of the DataObject object

The DataObject object provides the following method and properties that form developers can use to interact with an InfoPath external data source.

Name Description
Query method Reads data from the data adapter into the XML Document Object Model (DOM) associated with the DataObject object
DOM property Returns a reference to the XML DOM associated with the DataObject object
Name property Returns a string value indicating the name of the DataObject object
QueryAdapter property Returns a reference to a data adapter object

Overview of the data adapter objects

The data adapter objects provide different properties and methods that retrieve and submit data to external data sources; the data adapter that is associated with a DataObject object is dependent on the type of data source. InfoPath implements the following types of data adapter objects.

Name Description
ADOAdapter object Connects to ADO/OLEDB data sources; limited to Microsoft Access and Microsoft SQL Server.
WebServiceAdapter object Connects to Web services.
XMLFileAdapter object Connects to an XML files.
SharepointListAdapter object Connects to a Windows SharePoint Services 3.0 list.

Using the DataObjects collection and the DataObject object

The DataObjects collection is accessed through the DataObjects property of the XDocument object. For example, if you create a secondary data source named CityList that is based on a table in a Microsoft Access database, you can use the DataObjects collection to set a reference to the DataObject object that represents the external data source.

In the following code sample, the name of the secondary data source is passed to the Item property of the DataObjects collection, which returns a reference to the DataObject object, which, in this case, is associated with an ADOAdapter data adapter object. Using the QueryAdapter property of the DataObject object, the ADOAdapter data adapter object's Connection property displays the ADO connection string in a message box.

  function TestDataObjects()
{
   var objDataObject;
	
   // Set a reference to the specified data object.
   objDataObject = XDocument.DataObjects("CityList");
	
   // Display the connection information for the ADOAdapter object.
   XDocument.UI.Alert("Data Adapter: " + objDataObject.QueryAdapter.Connection);
	
   objDataObject = null;
}

To manipulate the data that is contained in a secondary data source, use the DOM property of the DataObject object to return a reference to the XML DOM containing the data. When you have the reference to the XML DOM, you can use any of its properties or methods to manipulate the data that it contains.