Tuesday, August 31, 2004

Creating a new folder in a mailbox if one doesn't exist

I've had this question from a few people that wanted to do this in a event sink, its acually really simple to do with a little bit of ADO/Exoledb code.

To create a folder in a mailbox using ADO you can use the adCreateCollection option in your record open statement. The following two lines would create a folder in a mailbox.

set rec = createobject("ADODB.Record")
rec.open "file://./backofficestorage/domain.com/MBX/mailbox/newfolder/", ,3,8192

The options included in the open statement are 3 which is adreadwrite and 8192 which is adCreateCollection (hex 0x2000)

If you try this code and the folder already exists you will get an error returned stating that the object already exists. So if you want to run the above code to create the folder only if the folder doesn't exist and not return an error if it does all you need to do is combine adCreateCollection with adOpenIfExists. eg

set rec = createobject("ADODB.Record")
rec.open "file://./backofficestorage/domain.com/MBX/mailbox/newfolder/", ,3,33562624

33562624 is the sum of adCreateCollection (hex 0x2000) and adOpenIfExists (hex 0x2000000)


Anonymous said...

That's handy. What's the chance of being able to set some properties on the folder as part of a slightly larger script?

E.g. If the folder exists then check it's homepage url, (perhaps report that to some location if different from expected - dealing with them on an exception basis). If the foldler doesn't exist, create it, and set the homepage url to some internal application or whatever, and set the property for this folder to display the homepage by default.

Is it also possible to set permissions on the folder so that users can't rename or delete (special folders in other words)?

What do you think - is all that possible? Your other scripts are great btw, I use a couple regularly (reporting type stuff). Cheers, Nick

Glen said...

Setting the homepage is do-able with this script. Changing the permissions so no-one can delete the folder im not sure about. I'll have a look and see what i can come up with

Anonymous said...

Is it possible to set the folder type? For example: I want to create a new folder in contacts. Now it appears as a common folder but it would be perfect if it appears as folder of the type contacts.
Thank you!!
Sepp (from Bavaria)

Glen said...

Sure all you need to do is set a few properties on the folder eg

' strFolderType Name
' MailItems IPF.Note
' ContactItems IPF.Contact
' AppointmentItems IPF.Appointment
' NoteItems IPF.StickyNote
' TaskItems IPF.Task
' JournalItems IPF.Journal

set rec = createobject("ADODB.Record")
rec.open "file://./backofficestorage/domain.com/MBX/newfolder/", ,3,33562624

' Specify the content class.
Rec.Fields("DAV:contentclass") = "urn:content-classes:folder"

' Specify the Outlook folder type.
Rec.Fields("http://schemas.microsoft.com/exchange/outlookfolderclass") = "IPF.Contact"



Pink said...

Thanks for this.