Thursday, February 24, 2005

Mail-Enabling a Public Folder via WMI (Exchange 2003)

One of the new WMI classes that comes with Exchange 2003 is the Exchange_PublicFolder Class. This is actually pretty useful when it comes to managing public folders via Code. For instance being able to propagate the folder settings to all of child subjfolers http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_wmiref_me_exchange_publicfolderpropagatesettings.asp . The other cool thing with this particular class is that a lot of the WMI properties are Read-Write. This means you can actually change setting and manipulate different aspect of public folders which you would usually need to use CDOEXM for. One of these is being able to mail-enable/disable a public folder. An example of mail-enabling a public folder via WMI would look like this. First you need to query the instances of public folders to find the folder you want to modify. I’ve used the Path property to search for the folder which is the normal path to the public folder but without the root suffix so if my public folder was http://server/public/myfolder my search string would need to be /myfolder/ (the end / is import). After that once you have made your modifications you need to call the Put_() method to commit the changes

The code looks like

On Error Resume Next
Dim cComputerName
Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_PublicFolder"
cComputerName = "servername"
cPublicFolderPath = "/foldertomailenable/"

strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//"& _
cComputerName&"/"&cWMINameSpace
Set objWMIServices = GetObject(strWinMgmts)
Set objPubInstances = objWMIServices.ExecQuery ("Select * From
Exchange_PublicFolder Where Path='" & cPublicFolderPath & "'")

For Each objExchange_PublicFolder in objPubInstances
WScript.echo objExchange_PublicFolder.Path
WScript.echo objExchange_PublicFolder.IsMailEnabled
objExchange_PublicFolder.IsMailEnabled = true
objExchange_PublicFolder.Put_()
Next

Tuesday, February 15, 2005

Creating one off SCL Rules with the Rule.dll

When using the IMF to do spam filtering you do get the occasional user where the SCL store threshold is set a little too high. Eg if you have the store threshold set to 6 and one user is getting a lot of SPAM that is getting a SCL of 5 having a one-off type trick to push these emails into the junk email folder can come in handy.

One way to do this is using the old Rule.dll COM component to create a server side rule in the inbox. Rule.dll has been around since Exchange 5.5 and because its mapi based it still works as it did back Exchange 5.5. There’s a great reference page with a lot samples on MSDN here

Some fast hard facts about using the rule.dll, Rules created using the rule.dll can’t be seen, deleted or modified using Outlook. And you still need to watch out for the 32 KB rules limit on a folder. There is a copy of the rule.dll included in one of the samples on CDOlive http://www.cdolive.net/download/ruleasp.zip or there should be a copy in the Platform SDK.

So to create a Rule base on the SCL value of an email you need to look at using the (PR_CONTENT_FILTER_SCL &H40760003) mapi property. So what the script does is first logs on the target mailbox finds the junk email folder by going though all the top level folders in the mailbox. Then creates a rule with a PropertyCondition so that if any mail is recived with a SCL of 5 then it gets moved into the junk email folder.

I’ve posted up a download copy of the code here
The script looks like


const REL_EQ = 7
Const ACTION_MOVE = 1
const CdoPR_CONTENT_FILTER_SCL = &H40760003
const SCL_VAL = 5

servername = "servername"
mailboxname = "mailboxalias"

Set objSession = CreateObject("MAPI.Session")

objSession.Logon "","",false,true,true,true,servername & vbLF & mailboxname
Set objRules = CreateObject("MSExchange.Rules")
objRules.Folder = objSession.Inbox
Set objInbox = objSession.Inbox

Set CdoInfoStore = objSession.GetInfoStore
Set CdoFolderRoot = CdoInfoStore.RootFolder
Set CdoFolders = CdoFolderRoot.Folders

bFound = False
Set CdoFolder = CdoFolders.GetFirst
Do While (Not bFound) And Not (CdoFolder Is Nothing)
If CdoFolder.Name = "Junk E-mail" Then
bFound = True
Else
Set CdoFolder = CdoFolders.GetNext
End If
Loop
Set ActionFolder = CdoFolder

Set importPropVal = CreateObject("MSExchange.PropertyValue")
importPropVal.Tag = CdoPR_CONTENT_FILTER_SCL
importPropVal.Value = SCL_VAL

Set importPropCond = CreateObject("MSExchange.PropertyCondition")
importPropCond.PropertyTag = CdoPR_CONTENT_FILTER_SCL
importPropCond.Operator = REL_EQ
importPropCond.Value = importPropVal

' Create action
Set objAction = CreateObject("MSExchange.Action")
objAction.ActionType = ACTION_MOVE
objAction.Arg = ActionFolder

' Create new rule
Set objRule = CreateObject("MSExchange.Rule")
objRule.Name = "SCL VAL Move"

' Add action and assign condition
objRule.Actions.Add , objAction
objRule.Condition = importPropCond

' Add rule and update
objRules.Add , objRule
objRules.Update

' Log off and cleanup
objSession.Logoff

Set objRules = Nothing
Set objSession = Nothing
Set importProp = Nothing
Set importPropVal = Nothing
Set objAction = Nothing
Set objRule = Nothing

Tuesday, February 08, 2005

Adding a public folder contact search option to OWA in Exchange 2003

One of the new features (among others) in Exchange 2003 OWA is the ability for the user to now search their own contacts folder when using the address book find in OWA 2003. One other location that can be used to store contacts especially if you want to share contacts among users is in a contacts folder in a public folder tree. So I was asked to look at making this available to search in OWA like the contacts folders.

This brings up the first question of how can I customize OWA I went digging though some old whitepapers and found this one about customizing OWA 2000 which is mostly still relevant to Exchange 2003. The low down from this whitepaper is the core HTML used to render OWA is stored in the wmtemplates.dll file which means that you can’t really mess with it. There are some other warnings in this whitepaper about any changes made to the .js, htc or xml files used by OWA are unsupported. But further on they give you an example of modifying one of the script files used by OWA to change the NAV bar. Siegfried Webber also has a sample on his blog that shows how to add a button to the NAV bar in OWA 2003.

What I learnt from these two sources was that although you can’t modify the OWA core code directly the scripts that contain all the page events that OWA loads can be modified and the document object can be used to insert your own custom code back into what gets rendered to the client.

So the first thing I had to do is work out which script was being used when you click the address book in OWA. This is where Ethereal (or some other packet capture program) is invaluable. Doing a capture I could see that file dlg_gal.js which is in the exchweb/version/controls directory was being used for events on this page and also I found that the ID for the drop down box I wanted to modify was selFindIn.

The next important thing was to come up with a function I could add to the script that would insert another option into this dropdown box. The document object was used to create a HTML Option tag, give it a value of 2 and then create a text node which contains the text that will be displayed in the box. The rest of the function ties the HTML together and then appends it into selFindIn tag block. The function looks like

// -------------------------------------------------------------------------------------------------------------
function addoptionitem()
{
var newoptionvalue = document.createElement("OPTION");
newoptionvalue.value = "2";
var oTextNode = document.createTextNode("Public Folder Contacts");
newoptionvalue.appendChild(oTextNode);
var objselfindin = g_winDocAll.item("selFindIn");
objselfindin.appendChild(newoptionvalue);
}
// -------------------------------------------------------------------------------------------------------------
To ensure this function will run a modification is also needed to the window.onload() event function defined in the script. Basically all that is involved was putting some code after the current last line in the function that would run my custom function in the onload event.

window.document.body.attachEvent("onkeydown", event_window_onkeydown);addMessage("", false);event_FindIn_change();g_winDocAll['DN'].focus();
// ------------------------------------------------------------------------------------------------------------------
addoptionitem();}
// ------------------------------------------------------------------------------------------------------------------

Once this is done I had a piece of functioning code that would add an extra item into the drop drown “find name in” box. The only thing missing is that it won’t do anything because there is nothing defined if the option value is 2 when the find button is pressed. Currently what happens when you select contacts and click find is that it does a WebDAV search of the contacts folder using the parameters entered. The only thing that needs to be changes in the current code to make this search a public contacts folder was the actually folder that is being searched. So I decided to go with reusing the functions that are already there for the contacts search and just modifying them so it would check and change that location it’s searching against if the option value is 2. To do this required editing another 2 existing functions. The first function that need to be modified was the event_button_find() this is the code that is run when someone presses the find button in the Web dialogue. All that I needed to modify was one if statement which currently said if the selFindIn value is 1 run this code so I changed it to if SelFindIn is 1 or 2 run this code. The following change was made

// ------------------------------------------------------------------------------------------------------------------
else if (selFindIn.value == 1 || selFindIn.value == 2)
// ------------------------------------------------------------------------------------------------------------------

Once this change was made this means that if Contacts or Public folder Contacts is selected it will run the same function. The next function in line is the queryGetContactsData() this function builds the webdav query of the contacts folder. Within this function I’ve added a if statement so if the selFindIn.value is 1 it runs the normal code it would usually run but if its two it will run one line different which changes the location it will perform the query from the contacts folder to the public folder contacts folder. There is a variable already defined that holds that URL of the public folder tree for the user so as long as there is a replica of your public contacts folder on this server all you need to do is append the name of the contacts folder you want to use. I defined a variable called g_sPublicContactsFolder to hold this information

// ------------------------------------------------------------------------------------------------------------------
if (selFindIn.value == 2){
var g_sPublicContactsFolder;
g_sPublicContactsFolder = g_sPublic + "PubContacts";
var oReq = new requestFactory(g_sPublicContactsFolder, "SEARCH", null,event_getData);}
else {
var oReq = new requestFactory(g_sContactsFolder, "SEARCH", null,event_getData);}
// ------------------------------------------------------------------------------------------------------------------

This part of the code you need to customize yourself in this example the contacts folder is called PubContacts and its just off the root of the public folder tree. g_sPublic holds the value of the Root public folder tree eg http://server/public/ so you just need to append the rest of the path.

That’s about it, but now you need to think about the problems of actually doing this. Firstly modifying this .js file isn’t supported. One reason for this is pretty obvious the files themselves are held in the /exchweb directory within a versions/controls directory. What version you have of these files is going to depend on what service pack you run and what hotfixes you have applied. For instance I currently have version 6.5.7232.34 of these files which came out of the KB 883543 hotfix. So if you don’t get where I’m going with this if you make changes to your files today apply a hotfix or the next SP your changes are going to get blown away. And because changes to these files don’t get documented when updates are released, you really need to start again and modify the new files sourced from the service pack/hotfix or risk OWA not working because of changes that have been made. Other problems you could have are that you need to have a replica of the public contact folder on the server that’s being uses for OWA. Also IE tends to cache this file so if you make an error modifying the script every time you make a change to the file you need to purge the file cache in IE (e.g. delete temporary internet files) so it will use the new version (It’s very easy to be fooled by this). You need to make sure you have got it right in a dev environment first and even then you may face problems with having the file cached by some clients.

I’ve put a downloadable copy of the 3 functions I modified and the 1 new function (you just need to add this at the bottom of the current script) here this is for illustration purposes only as I mentioned these files change regularly so if you do want to make changes you should make them yourself and understand what your doing. Also before you make any change, make sure you backup the existing dlg_gal.js file as this is your rollback plan.