How to: Use Microsoft.Office.Interop.InfoPath.SemiTrust Members That Are Not Compatible with InfoPath 2003

When you add code to a form template that was created with the Microsoft Office InfoPath 2003 Toolkit or create a new form template that works with the InfoPath 2003-compatible object model (as described in How to: Create a Managed Code Form Template Using the InfoPath 2003 Object Model), by default, Microsoft Office InfoPath 2007 will use a subset of the objects and members provided by the Microsoft.Office.Interop.InfoPath.SemiTrust namespace that are identical to those used by InfoPath 2003. This is done to provide compatibility with InfoPath 2003. However, the object model provided by the Microsoft.Office.Interop.InfoPath.SemiTrust namespace includes additional objects and members that provide new functionality that was added to InfoPath 2007.

For example, the PermissionObject and Permission interfaces provide entirely new information rights management functionality that is not available in InfoPath 2003. This, and other entirely new objects added to the Microsoft.Office.Interop.InfoPath.SemiTrust namespace are not available by default when you open or create managed code form template with InfoPath 2003-compatible object model.

Similarly, while the _XDocument2 interface provides the same functionality as InfoPath 2003; the _XDocument3 interface has been versioned to include additional properties and methods that were added in InfoPath 2007.

If you want to use objects and members that were added in InfoPath 2007 in a form template project created using the object model provided by the Microsoft.Office.Interop.InfoPath.SemiTrust namespace, you can do so, but code that uses these members will not be compatible with InfoPath 2003.

Note

All form templates with business logic created using the object model provided by the Microsoft.Office.Interop.InfoPath.SemiTrust namespace, whether they use objects and members compatible with InfoPath 2007 or not, are not supported for browser-enabled form templates deployed to Office Forms Server 2007 or Microsoft Office SharePoint Server 2007 with InfoPath Forms Services. Business logic for browser-enabled form templates must use the new InfoPath managed code object model provided by the Microsoft.Office.InfoPath namespace.

Example

Creating an XDocument or Application Object Variable to Access New Object Model Members

To access the new objects and members that are available in the Microsoft.Office.Interop.InfoPath.SemiTrust namespace, you must declare and cast object variables to the correct version of the interface that implements these members. By default, the thisXDocument and thisApplication variables access the InfoPath 2003-compatible versions of the corresponding _XDocument2 and _Application2 interfaces. To access the _XDocument3 and _Application3 interfaces that provide access to new functionality, you must declare an object variable of the _XDocument3 or _Application3 type, and then cast the object returned by the thisXDocument or thisApplication variable to the same type as shown in the following examples.

// Declare an object variable of type _XDocument3 and
// cast the object returned by the thisXDocument variable to
// the same type.
_XDocument3 thisXDocument3 = (_XDocument3)thisXDocument;
' Declare an object variable of type _XDocument3 and
' cast the object returned by the thisXDocument variable to
' the same type.
Dim thisXDocument3 As _XDocument3 = _
   DirectCast(thisXDocument, _XDocument3)
// Declare an object variable of type _Application3 and
// cast the object returned by the thisApplication variable to
// the same type.
_Application3 thisApplication3 = (_Application3)thisXDocument;
' Declare an object variable of type _Application3 and
' cast the object returned by the thisXApplication variable to
' the same type.
Dim thisDocument As _XDocument3 = _
   DirectCast(thisXDocument, _XDocument3)

Accessing a New Object From The XDocument or Application Object Variable Using an Accessor Property

After you have created a variable of the later version _XDocument3 or _Application3 type, you can use it to access an object or member that provides new InfoPath 2007 functionality.

The following example shows how to use an object variable of type _XDocument3 with the Permission accessor property to access the new Permission interface and its Enabled property to determine if permission settings are enabled for the form.

// Declare an object variable of type _XDocument3 and
// cast the object returned by the thisXDocument variable to
// the same type.
_XDocument3 thisXDocument3 = (_XDocument3)thisXDocument;

// Use the object variable to access the later version object and
// property.
thisXDocument.UI.Alert(thisDocument3.Permission.Enabled.ToString());
' Declare an object variable of type _XDocument3 and
' cast the object returned by the thisXDocument variable to
' the same type.
Dim thisXDocument3 As _XDocument3 = _
   DirectCast(thisXDocument, _XDocument3)

' Use the object variable to access the later version object and
' property.
thisXDocument.UI.Alert(thisDocument3.Permission.Enabled.ToString())

Accessing a Versioned Object and Casting to the Versioned Type

If an object that existed in the InfoPath 2003 object model has new properties or methods added to it, the object that implements those new members will have a name that is versioned.

For example, the ViewInfo object does not provide access to two new properties that are only available when using the versioned ViewInfo2 object: the Caption and HideName properties.

To access these properties, you must declare an object variable of type ViewInfo2 and cast the object returned by the ViewInfos property of the _XDocument3 object variable to the ViewInfo2 type as shown in the following example.

// Declare an object variable of type _XDocument3 and
// cast the object returned by the thisXDocument variable to
// the same type.
_XDocument3 thisXDocument3 = (_XDocument3)thisXDocument;

// Declare an object variable of type ViewInfo2 and cast the object 
// returned by the ViewInfos property to that type.
ViewInfo2 thisView = (ViewInfo2)thisXDocument3.ViewInfos["View2"];

// Display the value of the new HideName property.
thisXDocument3.UI.Alert(thisView.HideName.ToString());
' Declare an object variable of type _XDocument3 and
' cast the object returned by the thisXDocument variable to
' the same type.
Dim thisXDocument3 As _XDocument3 = _
   DirectCast(thisXDocument, _XDocument3)

' Declare an object variable of type ViewInfo2 and cast the object 
' returned by the ViewInfos property to that type.
Dim thisView As ViewInfo2 = _
   DirectCast(thisXDocument3.ViewInfos("View2"), ViewInfo2)

' Display the value of the new HideName property.
thisXDocument3.UI.Alert(thisView.HideName.ToString())