Implementing a Managed OnSave Event Sink
Topic Last Modified: 2006-06-12
The following samples catch the OnSave Method and write information to a log file. See Store Event Sink Bit Flags and Building Managed Event Sink DLLs for more information.
Visual Basic.NET
Example
Option Explicit On
Option Strict On
' Add project references to the System.EnterpriseServices, ADODB,
' Interop.Exoledb, and SignedExevtsnk .NET components.
Imports System.IO
Imports System.EnterpriseServices
Imports Exoledb = Interop.Exoledb
Imports ExevtsnkLib = SignedExevtsnk
Imports ADODB
Imports System.Reflection
Namespace ExchangeSDK.Snippets.VBNet
Public Class AsyncEvents
Inherits ServicedComponent
Implements Exoledb.IExStoreAsyncEvents
' Logfile path.
Private Const LOGFILE As String = "C:\\evtlog.txt"
Public Sub OnSave(ByVal pEventInfo As Interop.Exoledb.IExStoreEventInfo, _
ByVal bstrURLItem As String, _
ByVal lFlags As Integer) _
Implements Interop.Exoledb.IExStoreAsyncEvents.OnSave
' Variables.
Dim sr As StreamWriter
Dim rec As ADODB.Record
Dim conn As ADODB.Connection
' Open the log file, append text to file.
sr = File.AppendText(LOGFILE)
Try
sr.WriteLine("[VB.NET Event Sink] OnSave()")
' Write the URL of the item to the log.
sr.WriteLine("URL of item: " + bstrURLItem)
' Write the event flag to the log.
sr.WriteLine("lFlags: " & lFlags)
' Get the record object representing the item.
rec = New ADODB.Record
conn = New ADODB.Connection
' Open the connection
conn.Provider = "EXOLEDB.DataSource"
conn.Open(bstrURLItem, "", "", 0)
' Open the record.
rec.Open(bstrURLItem, _
conn.ConnectionString, _
ADODB.ConnectModeEnum.adModeRead, _
ADODB.RecordCreateOptionsEnum.adFailIfNotExists, _
ADODB.RecordOpenOptionsEnum.adOpenSource, _
"", "")
' Write the DAV:href property value of the item to the log.
sr.WriteLine("DAV:displayname value: " & CType(rec.Fields("DAV:displayname").Value, String))
sr.WriteLine("")
' Determine the cause of the OnSave event.
If (8 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_IS_DELIVERED)) Then
' A mail item was delivered.
sr.WriteLine("The EVT_IS_DELIVERED bit is set.")
ElseIf (256 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_MOVE)) Then
' An item was saved because of a move.
sr.WriteLine("The EVT_MOVE bit is set.")
ElseIf (512 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_COPY)) Then
' An item was saved because of a copy.
sr.WriteLine("The EVT_COPY bit is set.")
End If
sr.WriteLine("")
Catch ex As Exception
' Write exception info to the log.
sr.WriteLine("Exception message: " & ex.Message)
sr.WriteLine("")
End Try
' Clean up.
sr.Close()
rec.Close()
conn.Close()
End Sub
Public Sub OnDelete(ByVal pEventInfo As Interop.Exoledb.IExStoreEventInfo, _
ByVal bstrURLItem As String, _
ByVal lFlags As Integer) _
Implements Interop.Exoledb.IExStoreAsyncEvents.OnDelete
' Implement OnDelete code here.
End Sub
End Class
End Namespace
C#
Example
using System;
using System.Reflection;
using System.Diagnostics;
using Exoledb = Interop.Exoledb;
using ADODB;
using System.EnterpriseServices;
using System.IO;
namespace ExchangeSDK.Snippets.CSharp
{
class AsyncEvents : ServicedComponent, Exoledb.IExStoreAsyncEvents
{
// Logfile path.
private const string LOGFILE = "C:\\evtlog.txt";
public void OnSave(Exoledb.IExStoreEventInfo pEventInfo, string bstrURLItem, int lFlags)
{
// Variables.
StreamWriter sr;
ADODB.Record rec;
ADODB.Connection conn;
// Open the log file, append text to file.
sr = File.AppendText(LOGFILE);
try
{
sr.WriteLine ("[C# Event Sink] OnSave()");
// Write the URL of the item.
sr.WriteLine("URL of item: " + bstrURLItem);
// Write the event flag.
sr.WriteLine("lFlags: " + lFlags);
sr.WriteLine("");
// Get the record object representing the item.
rec = new ADODB.RecordClass();
conn = new ADODB.ConnectionClass();
// Open the connection.
conn.Provider = "EXOLEDB.DataSource";
conn.Open(bstrURLItem, "", "", 0);
// Open the record.
rec.Open(bstrURLItem,
conn.ConnectionString,
ADODB.ConnectModeEnum.adModeRead,
ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
ADODB.RecordOpenOptionsEnum.adOpenSource,
"", "");
// Write the DAV:href property value of the item to the log.
sr.WriteLine("DAV:displayname value: " + rec.Fields["DAV:displayname"].Value);
sr.WriteLine("");
// Determine the cause of the OnSave event.
if( 8 == (lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_IS_DELIVERED))
{
// A mail item was delivered.
sr.WriteLine("The EVT_IS_DELIVERED bit is set.");
}
if(256 == (lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_MOVE))
{
// An item was saved because of a move.
sr.WriteLine("The EVT_MOVE bit is set.");
}
if(512 == (lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_COPY))
{
// An item was saved because of a copy.
sr.WriteLine("The EVT_COPY bit is set.");
}
sr.WriteLine("");
// Clean up.
rec.Close();
conn.Close();
}
catch(Exception ex)
{
// Write exception info to the log.
sr.WriteLine("Exception message: " + ex.Message);
sr.WriteLine("");
}
// Close the stream writer.
sr.Close();
}
public void OnDelete(Exoledb.IExStoreEventInfo pEventInfo, string bstrURLItem, int lFlags)
{
// Implement OnDelete code here.
}
}
}