Sdílet prostřednictvím


Implementing a Managed OnSyncDelete Event Sink

Topic Last Modified: 2006-06-12

The following samples catch the OnSyncDelete 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 SyncEvents
   Inherits ServicedComponent
   Implements Exoledb.IExStoreSyncEvents

   ' Logfile path.
   Private Const LOGFILE As String = "C:\\evtlog.txt"

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

      ' Variables.
      Dim sr As StreamWriter
      Dim rec As ADODB.Record
      Dim dispEvtInfo As Exoledb.IExStoreDispEventInfo

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

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

         ' 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.
         dispEvtInfo = CType(pEventInfo, Exoledb.IExStoreDispEventInfo)
         rec = CType(dispEvtInfo.EventRecord, ADODB.Record)

         ' 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 phase of the OnSyncDelete event.
         If (16777216 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_SYNC_BEGIN)) Then

            ' Begin phase of the OnSyncDelete event.
            sr.WriteLine("The EVT_SYNC_BEGIN bit is set.")

         ElseIf (33554432 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_SYNC_COMMITTED)) Then

            ' Commit phase of the OnSyncDelete event.
            sr.WriteLine("The EVT_SYNC_COMMITTED bit is set.")

         ElseIf (67108864 = (lFlags And ExevtsnkLib.EVT_SINK_FLAGS.EVT_SYNC_ABORTED)) Then

            ' Abort phase of the OnSyncDelete event.
            sr.WriteLine("The EVT_SYNC_ABORTED bit is set.")
         End If

         ' Determine the cause of the OnSyncDelete 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()
      rec.Close()

   End Sub

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

      ' Implement OnSyncSave 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 SyncEvents : ServicedComponent, Exoledb.IExStoreSyncEvents
   {
      // Logfile path.
      private const string LOGFILE = "C:\\evtlog.txt";

      public void OnSyncDelete(Exoledb.IExStoreEventInfo pEventInfo, string bstrURLItem, int lFlags)
      {
         // Variables.
         StreamWriter sr;
         ADODB.Record rec;
         Exoledb.IExStoreDispEventInfo dispEvtInfo;

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

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

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

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

            // Get the record object representing the item.
            dispEvtInfo = (Exoledb.IExStoreDispEventInfo)pEventInfo;
            rec = (ADODB.Record)dispEvtInfo.EventRecord;

            // 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 phase of the OnSyncDelete event.
            if(16777216 == (lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_SYNC_BEGIN) )
            {
               // Begin phase of the OnSyncDelete event.
               sr.WriteLine("The EVT_SYNC_BEGIN bit is set.");
            }
            else if(33554432 == ( lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_SYNC_COMMITTED) )
            {
               // Commit phase of the OnSyncDelete event.
               sr.WriteLine("The EVT_SYNC_COMMITTED bit is set.");
            }
            else if(67108864 == (lFlags & (int)ExevtsnkLib.EVT_SINK_FLAGS.EVT_SYNC_ABORTED))
            {
               // Abort phase of the OnSyncDelete event.
               sr.WriteLine("The EVT_SYNC_ABORTED bit is set.");
            }

            // Determine the cause of the OnSyncDelete 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("");
         }

         // Clean up.
         sr.Close();
      }

      public void OnSyncSave(Exoledb.IExStoreEventInfo pEventInfo, string bstrURLItem, int lFlags)
      {
         // Implement OnSyncSave code here.
      }
   }
}