Platform SDK: Exchange Server

Sample Code: Enabling a Folder for Routing

The following is a simple example using Microsoft® Visual Basic® that illustrates how to make a folder routing-enabled. It creates and enables a new event binding for the routing engine in the current Inbox, and installs both a simple Map and the default VBScript actions (see Script Actions) onto that Binding message.

At a more detailed level, this code performs the following steps:

  1. Create a Session object (MAPI.Session) and log on to a MAPI session.
  2. Create an Events object (MSExchange.Events).
  3. Get the folder to be monitored (in the comment, the "event bindings folder associated with the current Inbox").
  4. Check for existing event bindings in the folder to be monitored, and if none exist, create one.
  5. Open the (hidden) message in which the event-binding information is stored.
  6. Read the script actions provided in the Routing.vbs file into a string and store them in a PR_EVENT_SCRIPT property.
  7. Create and populate a Map object (ExRt.Map). Save it on the hidden event-binding message. This example uses a function called MakeRow to help assemble individual lines in the Map object.
  8. Commit the changes and clean up.

For clarity the example omits most error handling. For example, it installs the event binding only if no other event bindings exist in the folder.

Const PR_EVENT_SCRIPT = &H7102001E
Const SCRIPT_LOCATION = "D:\SRC\ROUTING\ROUTING.VBS"

Dim oSession, oEvents, oMessage As Object
Dim oBindings, oBinding, oBoundFolder, oMap As Object

Set oSession = CreateObject("MAPI.Session")
oSession.logon

Set oEvents = CreateObject("MSExchange.Events")
oEvents.Session = oSession

' Get the event bindings folder associated with the current Inbox

Set oBoundFolder = oEvents.BoundFolder(oSession.Inbox, True)
Set oBindings = oBoundFolder.Bindings
If oBindings.Count > 0 Then
    MsgBox ("Existing Agents in this Folder. Press OK to quit.")
    Set oEvents = Nothing
    oSession.logoff
    End
Else
    Set oBinding = oBindings.Add
    oBinding.Name = "Routing Objects Sample"
    oBinding.Active = True
    oBinding.EventMask = 1 + 2 + 4 + 8     
    oBinding.HandlerClassID = "{69E64151-B371-11D0-BCD9-00AA00C1AB1C}"
    oBinding.SaveChanges
End If

' Get the (hidden) message associated with this event binding,
' so we can put some properties on it.

Set oMessage = oSession.Getmessage(oBinding.EntryID, Null)

' Read the standard script actions from the file ROUTING.VBS

Open SCRIPT_LOCATION For Input As #1
bstrEventScript = Input$(LOF(1), #1)
Close #1

' Store the script actions in PR_EVENT_SCRIPT

oMessage.Fields.Item(PR_EVENT_SCRIPT) = bstrEventScript
oMessage.Update

' Create a Map object and open it for read/write

Set oMap = CreateObject("ExRt.Map")

' Fill up the Map object with rows of activities
'  (The MakeRow function is defined below)

oMap.InsertActivity -1, _
   MakeRow( 1, "Send", 2, Array("Jane Smith", False, "Please review…", "<ATTACH>", False, False), 6 )

oMap.InsertActivity -1, MakeRow(2, "Wait", 0, Array(10080), 1) 

' Save the map into the hidden message object

oMap.SaveMap oMessage

' Note:  Must "dirty" the message by changing some property before
'  calling update.

oMessage.Subject = "Routing Object Samples"
oMessage.Update

' Must call SaveCustomChanges on the Binding object since we modified
'  custom properties on the event binding message.  The message object 
'  is the argument.

oBinding.SaveCustomChanges oMessage

' Commit changes to any bindings we've modified in this folder

oBoundFolder.SaveChanges

' Clean up

Set oEvents = Nothing
oSession.logoff

'==================================================
' The MakeRow function creates an "ExRt.Row" object and populates
'  its properties with information passed as arguments to the 
'  function.  It then returns the new object.
'
' Function MakeRow (
'    Id    -> Row Id (numeric)
'    Action     -> Row Action (string)
'    Flags    -> Action Flag (numeric)
'                    0 = engine intrinsic
'                   2 = script routine
'    Argv    -> Action arguments (array)
'    Argc    -> Number of elements in argv (numeric)
' )
' Returns the created Row object (ExRt.Row)
'==================================================
Function MakeRow ( ID, Action, Flags, Argv, Argc )
    Dim RTRow
    Dim RTRow = CreateObject("ExRt.Row")

    RTRow.ActivityID     = ID
    RTRow.Action        = Action
    RTRow.Flags        = Flags
    If Argc > 0 Then
        RTRow.SetArgs Argc, Argv
    End If

    ' Return the object
    Set MakeRow = RTRow
End Function