Implementing reusable OM that can be exposed to a task pane - David Airapetyan
in

InfoPath Dev

David Airapetyan

Implementing reusable OM that can be exposed to a task pane

When developing InfoPath solutions with a task pane, it is possible to call into form's code (business logic) via the window.external.Window.XDocument.Extension mechanism. For example, if you have a public method GetUserName() in your form code, you can call it as window.external.Window.XDocument.Extension.GetUserName().

What if you want to build a reusable library that can be exposed to the taskpane? It turns out that it's pretty easy to do:

1.       Create a regular C# library project, implement the OM as you see fit (make sure the assembly has a strong name)

2.       In project Assembly properties, mark the assembly as COM-visible

3.       Reference the library in your InfoPath project the usual way

4.       Create an OM member and expose it as a public property

5.       You can now call methods via the new OM property

The key here is step #2, marking the assembly as COM-visible. This allows Javascript to access its members and properties.

For example, if your library is called MyObjectModel and it has a method GetData(), you can expose and call it as follows:

In your FormCode.cs:

// Initialize it when appropriate, probably in OnLoad()
private
MyObjectModel _objectModel;

public MyObjectModel ObjectModel
{
  get {return _objectModel;}
} 

In your task pane code:

window.external.Window.XDocument.Extension.ObjectModel.GetData(); 

 

If you form needs to run in the Forms Server this becomes a little trickier because Forms Server business logic does not support member variables. Instead you have to store all state in the FormState object. Because of that you have to make your OM class serializable by adding [System.SerializableAttribute()] to the class definition.

Your public property then becomes this:

public ObjectModel ObjectModel
{
 
get { return (ObjectModel)FormState["ObjectModel"]; }
} 

This assumes you have initialized FormState["ObjectModel"] to an instance of your OM class, for example, during OnLoad().

 

Important: if you are using InfoPath 2007 OM, you need to mark both your business logic and the OM assemblies with the [assembly: AllowPartiallyTrustedCallers()]attribute.

 

Comments

No Comments

About davidair

I am a Computer Science graduate from McGill University in Montreal, Canada. I have over seven years of industry experience including almost five of them spent with the Microsoft Corporation where I shipped several versions of Microsoft Office InfoPath. Originally from Russia, I am fluent in several languages, including Russian, English, French, C#, C++ and Perl. I currently reside in Montreal and work for Qdabra Software.
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.