Thursday, November 10, 2005

Firing an event whenever a new Public folder is created

I was talking with someone this week about an issue where they wanted to maintain the functionality that existed pre going into native mode on Exchange 2003 that would allow public folders to be mail enabled and hidden from the Address list by default. Now there are a lot of good reasons why this is no longer the default action within Exchange but there are also some reasons why you may want this to still happen on a branch of your public folder tree. An event sink might be an obvious way to go about this but because an event sink can only have a shallow match scope on the default public folder tree there not a good choice for this job unless you only want this to work on one folder and not child folders.

What you can do however using the Exchange_PublicFolder WMI class is setup something that will listen for Instance creation events which will happen when a new public folder is created then check the path where the folder is being created and then if you want to have that folder mail-enabled use the WMI IsMailEnabled and PublishInAddressBook properties which are read/write. You could also monitor the _InstanceOperationEvent which would pickup every operation in a public folder but the number of events on busy exchange server would be excessive and you may would pay a large performance penalty. The creation event if setup correctly should only every catch new folder creations. A sample script to do this would look like the following the script checks to see if the folder is being created in the help desk branch of the public folder tree and then mail enables and hides the folder from the Gal via WMI.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\MicrosoftExchangeV2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'Exchange_PublicFolder' ")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
path = objLatestEvent.TargetInstance.path
if instr(path,"/help desk/") then
objLatestEvent.TargetInstance.PublishInAddressBook = false
objLatestEvent.TargetInstance.IsMailEnabled = true
objLatestEvent.TargetInstance.Put_()
wscript.echo "Mail Enabled Folder : " & path
end if
Loop

2 comments:

Anonymous said...

Hi Glen, your Exchange Dev Blog is by far the most valuable resource on the web, even better than Microsoft in certain degree. Great job!

I'm currently trying to find a way to listen on Public folder creation event and find your post is just what I'm looking for. However, Exchange server seems not to allow any Event Query on "Exchange_PublicFolder". No matter the event is "__InstanceCreationEvent", or "__InstanceOperationEvent", or "__InstanceDeletionEvent", the Exchange server always throws a "Quota violation" Exception. Do you have any idea?

Many thanks,

Irving

Glen said...

Hi Irving,

This is the one problem with this method this error is usually caused if you have a large number of public folders. What you have to do is put a Where clause in the WMI query so it limits the number of Public folder instances that are being included in the WMI query.