Within the InfoPath 2007, there is the option to handle events using an item called the Event Manager. Similar to the event receiver architecture provided by other facilities of SharePoint, the Event Manager provides mechanisms by which forms can provide and respond to event capturing. Writing code that is consumed by the Event Manager is done using Visual Studio Tools for Application (VSTA), and when choosing to program events off the InfoPath design surface it will attempt to open the VSTA IDE.
The Event Manager is called from the InternalStartup() method, which controls the events that are registered when a form is initially loaded. Each event that should be captured can be registered within the InternalStartup() method and then delegates can be constructed to encapsulate a reference to a method which handles the events tripped custom code.
There are several events that can be captured and managed when leveraging the Event Manager.
Event Capturing Available Through the Event Manager
|Control Event||OnClick||When a user clicks a control in InfoPath||ClickedEventHandler(object sender, Microsoft.Office.InfoPath.ClickedEventArgs e)|
|Form Event||OnSaveRequest||When a user saves an InfoPath Form||SaveEventHandler(object sender, Microsoft.Office.InfoPath.SaveEventArgs e)|
|Form Event||OnContextChange||When a user changes a Form Context, such as when a form is submitted||SubmitEventHandler(object sender, Microsoft.Office.InfoPath.SubmitEventArgs e)|
|Form Event||OnSign||When a user signs an InfoPath form with a digital signature||SignEventHandler(object sender, Microsoft.Office.InfoPath.SignEventArgs e)|
|Form Event||OnMergeRequest||When a user merges a set of InfoPath forms (merge operation)||MergeEventHandler(object sender, Microsoft.Office.InfoPath.MergeEventArgs e)|
|Form Event||OnSwitchView||When an InfoPath view is changed (switch view operation)||ViewSwitchedEventHandler(object sender, Microsoft.Office.InfoPath.ViewSwitchedEventArgs e)|
|Xml Event||OnBeforeChange||Before a passed XPath node is changed, wire an XML event||XmlChangedEventHandler(object sender, Microsoft.Office.InfoPath.XmlEventArgs e)|
|Xml Event||OnValidate||When an XPath node is being validated, wire an XML event||XmlChangingEventHandler(object sender, Microsoft.Office.InfoPath.XmlChangingEventArgs e)|
|Xml Event||OnAfterChange||After an XPath node is changed, wire an XML event||XmlValidatingEventHandler(object sender, Microsoft.Office.InfoPath.XmlValidatingEventArgs e)|
Using the InfoPath event handlers is as straight-forward as the event receiver/listener architecture that is present with SharePoint content types and list definitions. The event being captured must be registered within the InternalStartup() method by standard declaration. Wiring an event to display a message box telling the user they are switching views requires registering the event first.
By the same token, it is possible to validate the XML fields so a user entering null values into an InfoPath form is passed a message.
public void ProSharePoint2007Field_Validating(object sender, XmlValidatingEventArgs e)
Errors.Add(e.Site, “ProSharePoint2007Field “, “Null Values are not allowed!”);