The first step in writing scripts that run as part of the Exchange Event Service is to create an agent that acts as the event handler for certain folder events. To reach this interface, however, you are required to run certain versions of Outlook. To be a script writer for the Event Service, you must be running Outlook 97 version 8.03 or higher. The interface for creating new agents in Outlook is the Agents tab in the Properties dialog box, which was shown in Figure 12-5. To create a new agent, follow these steps:
Figure 12-6 The New Agent dialog box. This dialog box allows you to pick the events that your agent will fire on.
Figure 12-7 The Scheduled Event dialog box allows you to configure scheduled events for your agents.
Figure 12-8 A new script shown in Notepad. Notice how the new agent automatically contains four event procedures to handle the four events supported by the Event Service.
As mentioned earlier, the Event Service supports four different event types: message create, change, delete, and timer-based. To write a script that implements your functionality for these events, you must modify these four default stub subroutines:
When you write an Event Scripting Agent, you can also use JavaScript. In JavaScript, these would be the four functions:
Collaboration Data Objects (CDO), which you learned about in Chapter 11, represent the intrinsic object model for your scripts. When you are writing agents, the Event Service passes you some objects and variables that you can use to quickly figure out what item triggered the event and in what folder the item is located. To help you access these items quickly as well as access other Exchange Server items, the Event Service also passes you a pre-logged-on CDO session so that you do not have to log on to the Exchange Server yourself. The intrinsic objects passed to your script by the Event Service are discussed in the following sections.
The EventDetails.Session object represents the pre-logged-on CDO session for your script. The Event Service decides which identity to use for logging on to your script by using the identity of the author who most recently saved the script. This is important to consider for two reasons. First, the functionality of your application might depend on access to specific items in the Exchange Server Information Store. If the identity of the most recent author does not have access to this information, your script will not work.
Second, any mail you send from your script will use the name of the pre-logged-on CDO session because the Event Service is logging in as this user. The sent messages will also be saved in the Sent Items folder of that user. For these reasons, consider creating unique identities for your agents, and log on as these users to save your script. For example, if you are creating an expense report application, you might want to create a user named Expense Report Administrator and log on to your Exchange Server as that user. Then create and save your script using that identity. Any of the e-mail sent by the agent will appear to be from the Expense Report Administrator rather than from your personal account.
Since the CDO Session object is pre-logged-on, you can start accessing CDO objects directly from the EventDetails.Session object. It is a good idea in your script to assign the EventDetails.Session object to another variable for use throughout your script.
The EventDetails.FolderID variable contains the entry identifier of the folder that the event took place in. By using this variable with the CDO GetFolder method, you can quickly retrieve the correct folder for the event. Again, it is a good idea to assign this variable to another variable in your script.
The EventDetails.MessageID variable contains the entry identifier of the message that triggered the event. By using this variable with the CDO GetMessage method, you can quickly retrieve the exact message that the event corresponds to. Be aware, however, that timer events do not pass an EventDetails.MessageID variable because no message triggers the event; rather, an elapsed amount of time triggers the event. Keep this in mind when creating scripts, because an error related to EventDetails.MessageID for a timer event can be hard to track down when debugging.
In addition to using the CDO object library in your scripts, you can call other COM components by using the CreateObject method in VBScript. These components can include server-based object libraries such as ADO for database access and ADSI for directory access. You can even instantiate your own COM components developed using Visual Basic or Visual C++. There are two primary requirements for custom COM components to be used with the Event Service:
By remembering these two requirements, you can offload much of the work in your scripts to your COM components and include only the necessary script to instantiate your components.
To send errors from your component to the event script, use the Error.Raise method in your component. For debugging purposes, use the arguments of the Raise method to pass back the correct error number as well as the source and description of the error.
If your components instantiate other remote COM components, make sure to configure Distributed Component Object Model (DCOM) correctly so that the Windows NT account the Event Service is running under can correctly instantiate them. You can modify the permissions for DCOM using the DCOM Configuration program (dcomcnfg.exe).
In your objects, you can also create custom COM components that use the features of Microsoft Transaction Server (MTS) to make the components more scalable and robust. For example, components created with MTS can handle process isolation, security identity, resource pooling, and distributed transaction coordination. Your script can instantiate MTS objects using CreateObject in the same way it instantiates other types of objects.