HOWTO: Sample JScript for the Exchange Event Service

ID: Q198277

The information in this article applies to:

SUMMARY

The documentation for the Microsoft Exchange Event Service says that you can use JScript as well as VBScript to write agents to handle events, but it does not go into detail on using JScript. This article provides a sample of using JScript with the Event Service Scripting Agent.

MORE INFORMATION

Following is the sample script:

    <!-- Exchange Event Script -->

    <!--
        Folder_OnMessageCreated
        Message_OnChange
        Folder_OnMessageDeleted
        Folder_OnTimer
    -->
    <SCRIPT LANGUAGE=JScript>

    //'----------------------------------------------------------------
    //' Global Variables
    //'----------------------------------------------------------------
    var AMSession;
    var TargetFolder="";
    var TargetMessage="";
    var str="";

    //'----------------------------------------------------------------
    //' Event Handlers
    //'----------------------------------------------------------------
    function Folder::OnMessageCreated()
    {
       GetEventDetails();
       MakeResponseMessage("OnMessageCreated");
       str=str + "\n";
       Script.Response=str + "End of OnMessageCreated" +"\n";
    }

    function Message::OnChange()
    {
       GetEventDetails();
       MakeResponseMessage("Message_OnChange");
       str=str + "\n";
       Script.Response=str + "End of OnChange" +"\n";
    }

    function Folder::OnMessageDeleted()
    {
       MakeResponseMessage2("Folder_OnMessageDeleted");
       str=str + "\n";
       Script.Response=str+ "End of OnMessageDeleted" +"\n";
    }

    function Folder::OnTimer()
    {
       MakeResponseMessage2("Folder_OnTimer");
       str=str + "\n";
       Script.Response=str + "End of OnTimer" +"\n";
    }

    //'----------------------------------------------------------------
    //' DESCRIPTION: Retrieve session, folder, and message objects
    //'    from EventDetails
    function GetEventDetails()
    {
       // Get ID and Session object from EventsDetails
       var idTargetFolder = EventDetails.FolderID;
       var idTargetMessage = EventDetails.MessageID;
       AMSession=EventDetails.Session;

       if (AMSession==null)
       {
          str=str + "Failed to get session object ";
       }else{
          // Get target folder and message object
          TargetFolder=AMSession.GetFolder(idTargetFolder, null);
          TargetMessage=AMSession.GetMessage(idTargetMessage, null);
          str=str + " Get all event details" +"\n";
       }
    }

    //'----------------------------------------------------------------
    //' DESCRIPTION: Make a response message for OnMessageCreated and
    //'    Message_OnChange
    function MakeResponseMessage(strTypeEvent)
    {
       // Get Outbox folder
       var objOutboxFolder=AMSession.Outbox;
       if (objOutboxFolder==null)
       {
          str=str+" Failed to get outbox"+"\n";
       }else{
          var objMsgColl=objOutboxFolder.Messages;
          // Create a response message
          var msgResponse=objMsgColl.Add();
          if (msgResponse==null)
          {
             str=str+" Failed to create response message " +"\n";
          }else{
             msgResponse.subject= strTypeEvent + ": subject [" +
                              TargetMessage.subject + "] in folder [" +
                              TargetFolder.name + " ] triggered " +
                              " JScript method";
             var strText="About the triggered message:" +"\n";
             strText=strText + "Folder        " +
                     TargetFolder.Name + "\n";
             strText=strText + "Subject:      " +
                     TargetMessage.subject +"\n";
             strText=strText + "Text:         " +
                     TargetMessage.text +"\n";
             strText=strText + "Created: " +
                     TargetMessage.TimeCreated +"\n";
             strText=strText + "Time Send:    " +
                     TargetMessage.TimeSend +"\n";
             msgResponse.Text=strText;

             // Add recipient for the response message
             objRecipient=msgResponse.Recipients.Add();
             if (objRecipient==null)
             {
                str=str+ " Failed to add recipient" +"\n";
             }else{
                objRecipient.Name=TargetMessage.Sender;
                objRecipient.type=1;
                // Resolve recipient
                objRecipient.Resolve();
                // Send out the message
                msgResponse.Update();
                msgResponse.Send();
                str=str + "JScript has sent a message back to " +
                    AMSession.currentuser.name;
             }
          }
       }
    }

    //'----------------------------------------------------------------
    //' DESCRIPTION: Make a response message, for OnMessageDelete and
    //'    OnTimer
    function MakeResponseMessage2(strTypeEvent)
    {
       // Get session object and target folder ID from EventDetails
       AMSession=EventDetails.Session;
       var idTargetFolder=EventDetails.FolderID;
       TargetFolder=AMSession.GetFolder(idTargetFolder, null);
       // Create response message
       var objMessage = EventDetails.Session.Outbox.Messages.Add();
       if (objMessage==null)
       {
          str = str + "Failed to add a new message in outbox" +"\n";
       }else{
          objMessage.Subject = strTypeEvent +
                           " event triggered a JScript method in folder["
                           + TargetFolder.Name + "]";
          objMessage.Text = strTypeEvent +
                        " event triggered a JScript method in folder[" +
                        TargetFolder.Name + "]";
          var objOneRecip = objMessage.Recipients.Add();
          if (objOneRecip==null)
          {
             str=str + " Failed to add recipient " +"\n";
          }else{
             var objAddressEntry=EventDetails.Session.CurrentUser;
             objOneRecip.AddressEntry = objAddressEntry;
             objOneRecip.Type = 1;
             objOneRecip.Resolve();
             objMessage.Send();
             str=str + "JScript has been send a message back  to " +
                 EventDetails.Session.CurrentUser.Name;
          }
       }
    }

    </SCRIPT>

REFERENCES

For more information on Event Scripting, see the topic "Exchange Server Scripting Agent" in the Platform SDK Documentation.

For more information on using CDO (previously known as Active Messaging), see the topic "Microsoft Collaboration Data Objects Programmer's Reference" in the Platform SDK Documentation.

For more information on using JScript, see the topics "JScript Language Reference" and "JScript Tutorial" in the Platform SDK Documentation.

Additional query words: kbXchge kbCDO

Keywords          : kbCDO120 kbCDO121 kbXchge550 kbJScript kbMsg kbScript kbGrpMsg 
Version           : WINDOWS:1.2,1.21; WINNT:5.5
Platform          : WINDOWS winnt
Issue type        : kbhowto

Last Reviewed: April 7, 1999