Article ID: 555990 - Last Review: September 21, 2007 - Revision: 1.0 How to execute JScript code from an InfoPath 2007 browser-enabled form templateSUMMARYLearn how to execute JScript code from InfoPath browser-enabled form templates that are hosted in custom ASP.NET pages. AbstractThis article explains how to execute JScript code from InfoPath browser-enabled form templates that are hosted in custom ASP.NET pages. It assumes that you are familiar with designing and publishing InfoPath form templates, InfoPath Forms Services, the XmlFormView control, ASP.NET, JScript, and writing .NET code for InfoPath form templates. IntroductionWhile you cannot publish a form template that contains script to a server running InfoPath Forms Services, you can use the XmlFormView control to host a browser-enabled form template in a custom ASP.NET page and indirectly execute JScript code from the form template. To do this, you must:
Step 1: Using the NotifyHost Method to Send NotificationsYou can use the NotifyHost method of the XmlForm class to send notifications to the hosting environment of a form template. Perform the following steps to add a Button control and its event handler to a form template, and call the NotifyHost method:
Visual Basic In the previous example, the form will send the notification, “Message from InfoPath”, to the custom ASP.NET page when you click the button. While the notification must be a string, it need not be static. For example, you can retrieve the value of a field on the form and send this value as a notification. You can also pass an empty string to the NotifyHost method just to alert the ASP.NET page that an event occurred in the form. Note: Since the form template contains managed code, you will have to perform an administrator-approved deployment to publish the form template to a server running InfoPath Forms Services. Step 2: Hooking Up the NotifyHost Event to an Event HandlerThe NotifyHost method raises the NotifyHost event of the XmlFormView control. You must hook up and implement an event handler for the NotifyHost event before the ASP.NET page can receive notifications from the form. In the following example an XmlFormView control named XmlFormView1 has been added to a custom ASP.NET page. The control hosts a browser-enabled form template named MyFormTemplate.xsn, which was published by an administrator to the form template library, FormServerTemplates, of a site collection. ServerName is the name of the server that is running InfoPath Forms Services. To hook up the NotifyHost event of XmlFormView1 to an event handler named XmlFormView1_NotifyHost, you must add OnNotifyHost="XmlFormView1_NotifyHost" to the control tag of XmlFormView1. The following code shows how to do this: HTML After you hook up the event, you must implement the XmlFormView1_NotifyHost event handler in the code-behind of the ASP.NET page as follows: C# Visual Basic The NotifyHostEventArgs argument of the event handler has a property named Notification. This property contains the notification that the InfoPath form sends when you use the NotifyHost method. The following code retrieves and stores the notification in a string variable: C# Visual Basic Note: To use the NotifyHostEventArgs class, you must import the Microsoft.Office.InfoPath.Server.Controls namespace. Step 3: Executing JScript Code from the NotifyHost Event HandlerOnce you have hooked up the NotifyHost event to an event handler, you can write code in the event handler to register and execute JScript code. Example 1: Displaying a message box The following code displays a message box with the notification message that was sent from the InfoPath form. C# Visual Basic Example 2: Opening a new browser window The following code opens a new browser window and navigates to a specified URL. Here, the NotifyHost method is only used to alert the ASP.NET page that an event occurred in the InfoPath form, so the notification message that was sent is ignored. C# Visual Basic ConclusionYou cannot publish a form template that contains script to a server running InfoPath Forms Services. However, you can host a browser-enabled form template in an XmlFormView control on a custom ASP.NET page and use the NotifyHost method of the XmlForm class to send notifications from the InfoPath form to its host. This raises a NotifyHost event in the XmlFormView control, which you can then catch and handle in an event handler of the ASP.NET page. Finally, you can register and execute JScript code from within the event handler. COMMUNITY SOLUTIONS CONTENT DISCLAIMERMICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES. | Article Translations
|


Back to the top
