The Scripting Environment

 < Day Day Up > 

The scripting environment in InfoPath 2003 is the Microsoft Script Editor. If you have used the Microsoft Script Editor when creating script with other Microsoft Office applications such as Word, the layout and functionality will be familiar. You might also have used the Script Editor when adding client-side script to ASP .NET pages.

The Microsoft Script Editor is a powerful coding and debugging environment for both VBScript and JScript. This chapter describes several features of the Script Editor, but in the space available cannot attempt a comprehensive account of its capabilities.

Opening the Microsoft Script Editor

The appearance of the Script Editor after a successful installation is shown in Figure 17.1.

Figure 17.1. Appearance of Microsoft Script Editor after installation.

graphics/17fig01.gif

Access from InfoPath 2003 to the Microsoft Script Editor is available only in design mode. There are several ways to open the Script Editor. The most widely accessible is to select the Tools menu, Script, Microsoft Script Editor. You can use a keyboard shortcut, Alt+Shift+F11, as an alternative.

Alternatively, if you want to access the Script Editor specifically to code a handler for an OnLoad or OnSwitchView event, select Tools, Script, and then whichever of the two events is relevant to the form template you are designing.

To learn more about the OnLoad event, see "The OnLoad Event," p. 343 (Chapter 19).


To learn more about the OnSwitchView event, see "The OnSwitchView Event," p. 344 (Chapter 19).


In addition, you can access the Script Editor from the Properties windows of several InfoPath form controls. For example, on the General tab of the Button Properties window for a button form control, you are offered a Microsoft Script Editor button that opens the Script Editor (see Figure 17.2).

Figure 17.2. The Microsoft Script Editor button in the Button Properties window.

graphics/17fig02.gif

For other form controls, such as a text box, you can open the Script Editor by clicking the Data Validation button (on the Data tab) and then selecting an event in the Script section in the Data Validation window. Click the Edit button, and the Script Editor opens with an automatically created skeleton function or subroutine appropriate to the form control and the relevant InfoPath event. The following JScript code is created for the event handler for a text box form control that handles the OnValidate event ( assuming that JScript is the default scripting language):

 function msoxd_my_FirstName::OnValidate(eventObj) {  // Write your code here } 

You replace the comment line with JScript code appropriate to handling the OnValidate event.

SCRIPT EDITOR UNAVAILABLE

In the initial release of InfoPath 2003, the Microsoft Script Editor is unavailable for a small minority of users. To rectify the situation, open InfoPath and attempt to open the Script Editor (for example, select Tools, Script, Microsoft Script Editor). A dialog box will likely offer to install the Script Editor. Click Yes. If you have access to the CD or network location from which InfoPath was installed, the Script Editor is usually installed correctly after this procedure and then works normally.


Microsoft Script Editor Windows

As you saw in Figure 17.1, the Script Editor displays several panes by default:

  • The unlabelled Document pane contains scripting code.

  • The Document Outline pane displays the names of all functions and subroutines, enabling rapid navigation among them.

  • The Object Browser pane, if correctly configured, provides information about objects in the InfoPath object model.

  • The Project Explorer pane displays a list of the form templates that have a scripting file open in the Script Editor.

  • The Properties pane, when used in other settings, displays properties for objects. In InfoPath, the Properties window is displayed, but blank.

The Document Pane

The Document pane is where you will write your scripting code. Apart from times when I want to specifically make use of, for example, the Document Outline pane, I normally write code with the Document pane in full-screen mode. The techniques to toggle between full-screen mode and normal mode are described in the sidebar.

VIEWING CODE IN FULL-SCREEN MODE

You can view scripting code in full-screen mode using the Shift+Alt+Enter keyboard shortcut, which toggles between normal mode and full-screen mode. Alternatively, you can select the View menu and select the Full Screen option.


When the Script Editor is in full-screen mode (see Figure 17.3), a toolbar appears. Clicking on the toolbar returns the Script Editor to normal mode.

Figure 17.3. The Script Editor in full-screen mode.

graphics/17fig03.gif

Assuming you are familiar with the InfoPath Object Model (described in Chapter 18, "The InfoPath Object Model"), InfoPath events (described in Chapter 19, "Events in InfoPath"), and either JScript or VBScript, you can create your code.

The Document Outline Pane

The Document Outline pane is invaluable when you have large quantities of scripting code in a single file. Figure 17.4 shows the Document Outline pane increased beyond what would typically be a convenient size , with many functions for a form template visible.

Figure 17.4. The Document Outline pane in the Script Editor.

graphics/17fig04.jpg

The names of functions and subroutines in a script file are displayed in the same order that the corresponding code exists in the script file. For example, if you add a new event handler for the OnLoad event, it is added at the end of any existing code and is listed last in the Document Outline pane. There is no feature that automatically lists functions in alphabetical order.

The Object Browser

The Object Browser pane gives you convenient access to summary information about the objects, methods , and properties of the InfoPath object model.

After installation, the Object Browser might be empty. To populate the Object Browser with the information relevant to InfoPath, follow these steps:

  1. Click the Customize button (see Figure 17.5). The Selected Components window opens (see Figure 17.6).

    Figure 17.5. Clicking the Customize button with the Object Browser empty.

    graphics/17fig05.jpg

    Figure 17.6. The Selected Components window.

    graphics/17fig06.gif

    IF THE CUSTOMIZE BUTTON ISN'T DISPLAYED

    The Customize button is sometimes not displayed, even if the Object Browser pane is open. If this is the case, select View, Other Windows, Object Browser, and the Customize button appears.


  2. Highlight the Other Packages and Libraries option (highlighted in Figure 17.6). Click the Add button.

  3. After a pause of several seconds while the Script Editor searches your hard disk for relevant component information, the Component Selector window opens (see Figure 17.7).

    Figure 17.7. The Component Selector window.

    graphics/17fig07.jpg

  4. On a typical installation of Windows XP, for example, a huge list of possible components is listed. Scroll down, and highlight the Microsoft InfoPath 1.0 Type Library (highlighted in Figure 17.7). Click the Select button. The selected components are added to the Selected Components part of the Component Selector window (see Figure 17.8). There is also an option to add the Microsoft InfoPath 1.0 Client Library, but this doesn't seem to add anything in addition to the Type Library option.

    Figure 17.8. The selected components are added to the Selected Components section of the Components Selector window.

    graphics/17fig08.jpg

  5. I suggest that you also select the Microsoft XML 5.0 components. If you use Windows SharePoint Services, I suggest you also select the Microsoft SharePoint 2.0 Client Support Type Library.

    After you have selected the components of interest to you, confirming that they are displayed in the Selected Components part of the Component Selector window, click the OK button.

  6. You are returned to the Selected Components window, with the selected components added below the Other Packages and Libraries node (see Figure 17.9). Click OK. The Selected Components window closes .

    Figure 17.9. The selected components added to the Other Packages and Libraries node.

    graphics/17fig09.gif

The components you selected should now be visible in the Object Browser. To see the objects, methods, and properties that are available, click on the InfoPath node, for example. Clicking on an object, such as the Application object in the left part of the Object Browser, displays the relevant methods and properties in the right part of the Object Browser (see Figure 17.10).

Figure 17.10. Information on InfoPath objects visible in the Object Browser.

graphics/17fig10.jpg

Thus the Object Browser provides a convenient way to access objects, methods, and properties of the InfoPath object model. More detailed information about the InfoPath object model (introduced in Chapter 18) is provided in the Help files for the InfoPath client and the Script Editor.

The Project Explorer Pane

On the right side of the Script Editor, the Project Explorer is displayed in default configuration. The Project Explorer displays the InfoPath form templates that are open and lists the script file(s) associated with each form template (see Figure 17.11).

Figure 17.11. The Project Explorer displays open form templates.

graphics/17fig11.jpg

To open the Project Explorer if it isn't already, select View, Project Explorer. You can also use the keyboard shortcut Ctrl+Alt+L.

The Project Explorer provides a convenient way to switch between script files. To switch to a file of interest, simply double-click the filename. The desired script file is displayed in the Document pane.

Customizing the Script Editor

Many options allow you to customize the Script Editor to suit your preferences. Some of the more useful customization options are described in this section.

Customization of the Script Editor is carried out using the Options window, which you open by selecting Tools, Options. In the white area to the left of the Options window are four folders (see Figure 17.12) labeled Environment, Text Editor, Debugging, and HTML Designer.

Figure 17.12. The Options window of the Script Editor.

graphics/17fig12.gif

Further options are available inside each of the four folders. I will mention only a few of the customization options here.

Tabbed Document or Multiple Document Interface

The Script Editor displays script files in the Document pane as tabbed documents by default (see Figure 17.13). In tabbed document mode, you can switch between code files by clicking on the relevant tab. Using the Project Explorer in practice is often easier because when double-clicking on a filename, you can see the form template to which the file belongs.

Figure 17.13. Three form templates with script files in tabbed documents mode.

graphics/17fig13.jpg

Alternatively, you can select multiple document interface mode, which means no tab is visible in the Main Code pane (see Figure 17.11).

To switch between windows in multiple document interface mode, select the Window menu and then the script file of interest. Unfortunately, the menu is not particularly helpful because if you have multiple files named script.js open at the same time, there is no easy way to differentiate between the files on the Window menu. However, the final option on the Windows menu is labeled Windows. Selecting that option opens the Windows window (see Figure 17.14), which allows you to select a script file while seeing which form template it belongs to.

Figure 17.14. The Windows window when using multiple document interface mode.

graphics/17fig14.gif

I find a more helpful way to switch between script files when in multiple document interface mode is to use the Project Explorer. Each script.js or script.vbs file is shown as belonging to a named InfoPath form template. Double-clicking the desired script file displays it in the Script Editor.

To toggle between tabbed document and multiple document interface modes, select Tools, Options. In the Options window, select the Environment folder, then select the General option, and then select the Tabbed Documents or MDI Environment option button as appropriate (see Figure 17.15). You need to restart the Script Editor for any change to take effect.

Figure 17.15. The Options window in Microsoft Script Editor.

graphics/17fig15.gif

Line Numbers

I find it very useful to have line numbers displayed, because JScript error messages will typically refer you to a line number.

OPTIONS WINDOW

The Options window in the Microsoft Script Editor remembers which folder in the left area of the Options window you used last before you closed the window. This can be useful if you want to return to the options you were previously adjusting, or confusing if you expect to see general Environment options displayed when the Option window opens.


To display line numbers for all languages in the Script Editor, open the Options window, open the Text Editor folder in the left area of the Options window, and select the All Languages folder (which is inside the Text Editor folder). Line numbers can be turned on (or off) using a check box in the Display section of the Options window (see Figure 17.16).

Figure 17.16. Turning line numbers on and off using the Options window.

graphics/17fig16.gif

Scrollbars

Scrollbars are, in my opinion, essential when using the Script Editor to edit InfoPath scripting files. For those developers who prefer to navigate via the keyboard, there are options to turn off both horizontal and vertical scrollbars. If you turn these off, you will have to navigate all but the most trivial InfoPath script files by extensive use of the cursor keys.

Just in case you or someone else accidentally turn scrollbars off, you need to know how to restore them. Open the Options window, open the Text Editor folder, and click on the General option. In the Display section of the Options window, you will see two check boxes labeled Vertical Scrollbar and Horizontal Scrollbar. I suggest that both check boxes should be checked (see Figure 17.17).

Figure 17.17. Check boxes to ensure that scrollbars are displayed.

graphics/17fig17.gif

Additional Languages

InfoPath 2003 is, as discussed in earlier chapters, primarily an enterprise forms tool. Therefore, the absence of .NET functionality in the initial release version of InfoPath 2003 was a surprise to some potential users.

It is possible to use the Common Language Runtime (CLR) from InfoPath. The InfoPath Software Development Kit (available for download from http://www.microsoft.com/downloads/details.aspx?FamilyID=351f0616-93aa-4fe8-9238-d702f1bfbab4&displaylang=en) includes a CLR sample form template.

ADDITIONAL CLR SUPPORT

Microsoft has indicated in recent Webcasts that CLR and managed code support for InfoPath will be provided in a release scheduled to be available around mid-2004. Currently it is unclear if that will be made available via a Service Pack or other route. It is also unclear which version(s) of Visual Studio will be necessary. I suggest you visit the InfoPath section of the Microsoft Web site to check the current situation.


Using Bookmarks

When you create script code for the production of InfoPath form templates, you will likely use substantial amounts of JScript or VBScript code. Navigating around large quantities of text, particularly when you might need to scroll horizontally to find the relevant part of an associated function or other code, can be tedious . The Microsoft Script Editor allows you to insert temporary bookmarks to enable you to move easily between two pieces of code (see Figure 17.18).

Figure 17.18. The Bookmarks button on the Script Editor toolbar.

graphics/17fig18.jpg

To the right of the button that toggles a single bookmark on and off are further buttons that navigate, one bookmark at a time, forward or backward. The right-most of the four default bookmark buttons clears all bookmarks from a script file.

BOOKMARK KEYBOARD SHORTCUTS

There are several keyboard shortcuts to manipulate or navigate Script Editor bookmarks:

To toggle a bookmark on or off on a line, press Ctrl+K, Ctrl+K (press Ctrl+K twice).

To navigate to the next bookmark, press Ctrl+K, Ctrl+N.

To navigate to the previous bookmark, press Ctrl+K, Ctrl+P.

To remove all bookmarks, press Ctrl+K, Ctrl+L.


Script Editor Help

The Microsoft Script Editor comes with copious Help Files that cover a large amount of information relevant to coding behind the scenes in InfoPath. Not all of the help information provided is directly relevant to InfoPathsome of it deals more with the use of the Script Editor with HTML files that contain scripting code.

To access Script Editor Help files, select Help, Microsoft Script Editor Help, or press F1. If the cursor is in the Document pane and you have written some code the Script Editor can recognize, you will be taken to the help for that particular object or method. For example, typing XDocument at the beginning of a line of code and then pressing F1 should open a Help window that provides information about the properties and methods of the XDocument object.

ACCESSING HELP DOESN'T ALWAYS WORK

From the Script Editor, you ought to be able to access the Script Editor Help window by pressing the F1 key. However, I have found that sometimes the F1 key doesn't work. If you experience this problem, you can open the Script Editor Help window by choosing the Help menu in the Script Editor and selecting Microsoft Script Editor Help.


The Script Editor Help includes several "books" of help files covering general Script Editor information, an InfoPath Developers Reference (with nested books providing an InfoPath object model reference and a reference on the manifest), MSXML 5.0, JScript, and VBScript (see Figure 17.19).

Figure 17.19. The Script Editor Help window.

graphics/17fig19.gif

 < Day Day Up > 


Microsoft Office InfoPath 2003 Kick Start
Microsoft Office InfoPath 2003 Kick Start
ISBN: 067232623X
EAN: 2147483647
Year: 2004
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net