Share via


Implementing a Managed OnDelete Event Sink

Topic Last Modified: 2006-06-12

The following samples catch the OnDelete 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 OnDelete(ByVal pEventInfo As Interop.Exoledb.IExStoreEventInfo, _
                       ByVal bstrURLItem As String, _
                       ByVal lFlags As Integer) _
              Implements Interop.Exoledb.IExStoreAsyncEvents.OnDelete

      ' Variables.
      Dim sr As StreamWriter

      ' Open the log file, append text to file.
      sr = File.AppendText(LOGFILE)

      Try
         sr.WriteLine("[VB.NET Event Sink]   OnDelete()")

         ' 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)

         ' Determine the cause of the OnDelete event.
         If (2 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_IS_COLLECTION)) Then

            ' The item is a collection.
            sr.WriteLine("The EVT_IS_COLLECTION bit is set.")

         ElseIf (16 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_SOFTDELETE)) Then

            ' Soft delete of the item.
            sr.WriteLine("The EVT_SOFTDELETE bit is set.")

         ElseIf (32 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_HARDDELETE)) Then

            ' The item was deleted permanently.
            sr.WriteLine("The EVT_HARDDELETE bit is set.")

         ElseIf (256 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_MOVE)) Then

            ' The item was deleted as part of a move.
            sr.WriteLine("The EVT_MOVE 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()

   End Sub

   Public Sub OnSave(ByVal pEventInfo As Interop.Exoledb.IExStoreEventInfo, _
                     ByVal bstrURLItem As String, _
                     ByVal lFlags As Integer) _
              Implements Interop.Exoledb.IExStoreAsyncEvents.OnSave

      ' Implement OnSave 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
{
   public class AsyncEvents : ServicedComponent, Exoledb.IExStoreAsyncEvents
   {
      // Logfile path.
      private const string LOGFILE = "C:\\evtlog.txt";

      public void OnDelete(Exoledb.IExStoreEventInfo pEventInfo, string bstrURLItem, int lFlags)
      {
         // Variables.
         StreamWriter sr;

         // Open the log file, append text to file.
         sr = File.AppendText(LOGFILE);

         try
         {
            sr.WriteLine ("[C# Event Sink]   OnDelete()");

            // Write the URL of the item.
            sr.WriteLine("URL of item: " + bstrURLItem);

            // Write the event flag.
            sr.WriteLine("lFlags: " + lFlags);
            sr.WriteLine("");

            // Determine the cause of the OnDelete event.
            if(2 == (lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_IS_COLLECTION))
            {
               // The item is a collection.
               sr.WriteLine("The EVT_IS_COLLECTION bit is set.");
            }

            if(16 == (lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_SOFTDELETE))
            {
               // Soft delete of the item.
               sr.WriteLine("The EVT_SOFTDELETE bit is set.");
            }

            if(32 == (lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_HARDDELETE))
            {
               // The item was deleted permanently.
               sr.WriteLine("The EVT_HARDDELETE bit is set.");
            }

            if(256 == (lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_MOVE) )
            {
               // The item was deleted as part of a move.
               sr.WriteLine("The EVT_Move bit is set");
            }

            sr.WriteLine("");

         }
         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 OnSave(Exoledb.IExStoreEventInfo pEventInfo, string bstrURLItem, int lFlags)
   {
      // Implement OnSave code here.
   }
}
}