HOW TO: Sample JScript for the Exchange Event Service (198277)



The information in this article applies to:

  • Collaboration Data Objects (CDO) 1.2
  • Collaboration Data Objects (CDO) 1.21
  • Microsoft Exchange Server 5.5

This article was previously published under Q198277

SUMMARY

This step-by-step article provides a sample JScript for use with the Event Service Scripting Agent.

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.

back to the top

Sample Code

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>
				
back to the top

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 (1.2, 1.21), 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.

back to the top

Modification Type:MinorLast Reviewed:3/4/2004
Keywords:kbHOWTOmaster kbMsg kbScript KB198277