Recipe 9.7. Creating and Deleting Public FoldersProblemYou want to create or remove public folders from a public folder tree. SolutionUsing a graphical user interfaceTo create a folder, do the following:
To delete a folder, do the following:
Using VBScript' This code creates a folder with the specified name at the top level ' of the MAPI TLH. ' ------ SCRIPT CONFIGURATION ------ strFolderName = "<folderName>" ' e.g., "Bad Jokes" ' ------ END CONFIGURATION --------- ' Get the default domain name. This is not necessarily ' the same as the AD domain name, which is why we have to ' get it from the recipient policy object Set objDefaultPolicy = GetObject( _ "LDAP://<dcName>/cn=default policy,cn=recipient policies," &_ "cn=<orgName>,cn=microsoft exchange,cn=services," &_ "cn=configuration,dc=<domain>,dc=<TLD>") ' get all of the proxy addresses in the default recipient ' policy. Find the default SMTP policy and that'll tell us ' what the default Exchange domain is strProxies = objDefaultPolicy.Get("gatewayProxy") For Each proxyAddr In strProxies If (Left(proxyAddr, 5) = "SMTP:") Then strStorageName = Right(proxyAddr, Len(proxyAddr)-InStr(proxyAddr, "@")) End if Next ' Build the path to the storage object. strStorageName = "file://./backofficestorage/" + strStorageName + "/" ' Create the folder.object strFolderPath = strStorageName + "public folders/" + strFolderName Set theFolder = CreateObject("cdo.folder") ' Set the folder properties. With theFolder .Description = "Root folder for " + strFolderName .ContentClass = "urn:content-classes:folder" .fields("http://schemas.microsoft.com/exchange" &_ "/outlook/outlookfolderclass") = _ "IPF.Folder" .Fields.Update .DataSource.SaveTo strFolderPath End With WScript.Echo "Done creating folders." DiscussionIf your account has appropriate permissions, you can also create and delete public folders directly from within Outlook; you do this using the same process you'd use for creating or deleting any other kind of folder with a few exceptions. First, you can only create and remove public folders while you're online with the server. Even though cached Exchange mode has an option to automatically cache posted items in public folders on your favorites list, you can't add or remove folders. Second, when you create a new folder, Outlook asks you to specify what kind of items (contacts, calendar items, notes, or messages) will go in the folder; ESM doesn't care, so it doesn't ask. Using VBScriptThe code creates a new folder by creating a CDO folder object and assigning it a path in the MAPI public folder tree. To do this, we have to find the correct default domain name for Exchange. This name may or may not be the same as the Active Directory domain name; the simplest way to check is to look at the primary SMTP proxy address on the default recipient policy, so that's what the script does. Once the script has the correct name, it can specify the location of the folder and create it using CDO. This will only work for accounts that have permission to create top-level public folders. There's no good way to use the same interfaces to delete a public folder, since the data in the folder is only permanently deleted when you delete the last replica of that folder. If you try to use this method to create a folder that already exists, you'll get error 0x80040e98, which isn't well-documented on MSDN (or anywhere else). The associated error string will say that the requested object cannot be shared; what it should say is that the object exists already. See AlsoRecipe 9.3 for getting and setting permissions, Recipe 9.8 for mail-enabling or mail-disabling a folder, Recipe 9.11 for controlling who can create top-level public folders, and MSDN: CDO |