Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Sample JScript for the Exchange Event Service


View products that this article applies to.

This article was previously published under Q198277

↑ Back to the top


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.

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


Keywords: KB198277, kbscript, kbmsg, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 198277
Revision : 4
Created on : 3/4/2004
Published on : 3/4/2004
Exists online : False
Views : 408