Tuesday 6 March 2007

Playing with Event Sinks

The first task I had on my list was to understand Exchange Event Sinks and to try and implement a script so that changes made to a public folder triggered 'an event'.

I did a lot of browsing on the web and came across lots of articles that eventually pieced together. The promising starting point came from Glen's Exchange Dev Blog, http://gsexdev.blogspot.com/2004/06/public-folder-rss-feed-event-sink.html. This looked like an ideal place to start and soon led me to a couple of other articles for those vital first steps, one of the best being another of Glen's, http://www.outlookexchange.com/articles/glenscales/wssevtar.asp.

So the first thing I had to do was register the Script Host Sink on my Exchange server as per article http://support.microsoft.com/kb/q264995/. Essentially do the following:

Copy the ESHMTS.VBS from your Exchange CD (mine was located in Setup\I386\Exchange) to your ExchSrvr\Bin directory on the server.

Bring up a command prompt and navigate to your ExchSrvr\Bin folder then enter the following:
regsvr32.exe exodbesh.dll
regsvr32.exe exodbprx.dll
cscript eshmts.vbs install

So now that the COM bit is done we can start playing with some scripts. I need to capture whenever an item is created, changed or deleted. A bit more research discovered that this is just down to two events
ExStoreEvents_OnSave
and
ExStoreEvents_OnDelete

I utilised Glen's example and expanded in to include the OnDelete as well:
<SCRIPT LANGUAGE="VBScript">
Sub ExStoreEvents_OnSave(pEventInfo, bstrURLItem, lFlags)
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("D:\DataApps\Exchange\Time.txt",8,true)
wfile.write vbNewLine & "Saved:" & (bstrURLItem)
wfile.close
set fso = nothing
set wfile = nothing
End Sub

Sub ExStoreEvents_OnDelete(pEventInfo, bstrURLItem, lFlags)
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("D:\DataApps\Exchange\Time.txt",8,true)
wfile.write vbNewLine & "Deleted:" & (bstrURLItem)
wfile.close
set fso = nothing
set wfile = nothing
End Sub
</SCRIPT>

All this script is doing is dumping the URL to the item out to a text file whenever either event is fired. All well and good, but what we need to do next is register this script, which we can't do until we have the script to do the registration, which isn't there by default!! We have to download and install the Exchange SDK, which can be found here, http://www.microsoft.com/downloads/details.aspx?FamilyID=32774e09-4984-458e-bdb7-ed2bb356bd27&DisplayLang=en

Once you've downloaded and installed the SDK you can then copy the three files you need from, in my case, C:\Program Files\Exchange SDK\SDK\Support\OLEDB\Scripts to your development folder.

So we have our script and we have the script to register it, so we just need to put the two together. To make mine work I ran the following from the command prompt:
cscript regevent.vbs ADD "onsave;ondelete" ExOleDB.ScriptEventSink.1 "file://./backofficestorage/ctscomputing.com/public folders/customer services/time/evtsnk1" -file d:\DataApps\Exchange\Time.vbs

This registers my script Time.vbs with the OnSave and OnDelete events in the public folder Customer Services\Time

I thought I'd better also find out how to unregister these events, which the following line does:
cscript regevent.vbs delete file://./backofficestorage/ctscomputing.com/public folders/customer services/time/evtsnk1

I then proceeded to create, change and delete some items, which triggered the events and generated the following text file:
Saved:file://./backofficestorage/ctscomputing.com/Public Folders/Customer Services/Time/Test-126958582.EML
Saved:file://./backofficestorage/ctscomputing.com/Public Folders/Customer Services/Time/Test-126958582.EML
Deleted:file://./backofficestorage/ctscomputing.com/Public Folders/Customer Services/Time/Test-126958582.EML

Woohoo, I have an Exchange Event Sink working. I know it's not doing anything useful at the moment, but at least it's a step in the right direction.

Next thing on the agenda is to get some useful information from these links, but that's for another time!