다음을 통해 공유


Implementing an OnSave Event Sink

Topic Last Modified: 2006-06-12

The following code receives the OnSave event and returns information about it. See Store Event Sink Bit Flags for more information.

Example

Visual Basic

Private Sub IExStoreAsyncEvents_OnSave(ByVal pEventInfo As Exoledb.IExStoreEventInfo, ByVal bstrURLItem As String, ByVal lFlags As Long)

    Dim FSO         As Object
    Dim EvtLog      As String
    Dim EvtFile
    Dim ADODBRec    As New ADODB.Record

'log file
    EvtLog = Environ("SystemDrive") & "\OnSave.log"

'Creates new log file %SystemDrive%\OnSave.log or opens it if exists
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set EvtFile = FSO.OpenTextFile(EvtLog, 8, True)

'Append incoming event info into log file
    EvtFile.WriteLine ("[VB Event Sink]          OnSave()")
    EvtFile.WriteLine ("  URL Item:              " & bstrURLItem)
    EvtFile.WriteLine ("  lFlags:                " & "0x" & Hex(lFlags))

'Small sample that shows how to use ADO Record Object and Fields inside events
    ADODBRec.Open bstrURLItem, , adModeRead, adFailIfNotExists
    If Err.Number <> 0 Then
        EvtFile.WriteLine "Failed to open Target URL: " & bstrURLItem & vbCrLf & "Error: " & Err.Description
    Else
        EvtFile.WriteLine ("  DAV:Displayname Value: " & ADODBRec.Fields("DAV:displayname").Value)
    End If

'To determine cause of OnSyncSave

    'Case 1: EVT_IS_DELIVERED
    If lFlags And EVT_IS_DELIVERED Then
        'Perform your tasks
        'OnSave for delivered mail item
        EvtFile.WriteLine ("  Flag contains EVT_IS_DELIVERED bit set")

    'Case 2: EVT_MOVE
    ElseIf lFlags And EVT_MOVE Then
        'Perform your tasks
        'OnSave for moved item
        EvtFile.WriteLine ("  Flag contains EVT_MOVE bit set")

    'Case 3: EVT_COPY
    ElseIf lFlags And EVT_COPY Then
        'Perform your tasks
        'OnSave for copied item
        EvtFile.WriteLine ("  Flag contains EVT_COPY bit set")

    End If

    'Check if it is a folder notification
    If lFlags And &H2 Then
        'Perform your tasks
        'OnSave for a folder
        EvtFile.WriteLine ("  Flag contains EVT_IS_COLLECTION bit set")
    End If

    EvtFile.WriteBlankLines (1)

    'Use the sink context (custom contextual info saved in the registration item)
    Dim case_switch As Integer
    Dim notify_group As String

    Const customnamespace = "mycustomnamespace:eventsinks/notifyingevents/"
    Dim propname As String

    propname = customnamespace + "caseswitch"
    case_switch = ADODBRec.Fields(propname)

    propname = customnamespace + "notifygroup"
    notify_group = ADODBRec.Fields(propname)

    'Call some program here to use the custom data to notify people of an event
    'Something like: handle_notifyevent(case_switch, notify_group)

'Before Quit
    EvtFile.Close
    Set FSO = Nothing

End Sub