Thursday, August 23, 2007

Adding Categories to the Master categories list in Outlook 2007 with a CDO 1.2 script

This is a two part post that I thought I’d separate this idea came from someone who asked about how you could group messages by their attachment types. Normally this would be a pretty hard thing to achieve manly because of the way attachments are stored doesn’t lend itself well to being grouped by in a search folder or an Outlook Customize view. But this got me thinking about what if you could use the new colored category feature in Outlook 2007 instead. Eg for each attachment type you have a separate color and Label. This works out pretty cool because you go from being able to look at your email and seeing that there is an attachment eg the paper clip icon to being able to look at a message and if you see a blue category mark you know that message has got an attachment and it’s a word document if it’s a green mark you know it’s a Excel document. You can then also create Views or Search folders based on the attachment categorization you could also could create an event sink or Notification app to assign the category to new items when they arrive.

To start with this idea however I first needed to make changes to the master categories list in Outlook 2007 while doing this from Outlook 2007 is the easy method I wanted to do this programmatically instead. The master categories list is held in a hidden message (in the associated contents collection) with a message class of “IPM.Configuration.CategoryList”. On this message there is a binary Mapi property 0x7C080102 which holds the category list which in is XML format. So to modify the list you need some code that will first read this property I used CDO 1.2 so when you read the property with CDO you get back a hex string representation of the Binary property. To make use of this the Hex needs to be converted to a String which will represent the XML document. I then loaded the XML back into the XMLDom object and used the clonenode method to copy one of the existing nodes and then modified the necessary properties for the new category I wanted to add. The three important bits of information you need to set are the Name which is the keyword value you going to use for you category. An integer for the color you want the category to be and a unique GUID value. To stop duplicates there’s some code to check if the name of Guid already exists in the XML document if so it doesn’t try to add another node. To write the modified XML back to the property theres a function that coverts the XML String back to hex.

The code will only work to modify an existing category list it won’t create one from scratch. To run the code you need to supply the servername and mailbox name of the mailbox you want to run it against as commandline parameters eg

Cscript modcats.vbs servername mailboxname

I’ve put a download of the scrip here the script itself looks like

snServername = wscript.arguments(0)
mbMailboxName = wscript.arguments(1)

' Word Documents
ReDim wdattah(1)
wdattah(0) = "{DB13F464-2FAA-48F2-8D1B-ADB5ED4FD1F7}"
wdattah(1) = 22
'Excel Attachments
ReDim edattach(1)
edattach(0) = "{D549D2BB-E1BF-47DE-B713-784771F059A1}"
edattach(1) = 19
'PowerPoint Attachments
ReDim pptattach(1)
pptattach(0) = "{1E8ADCFB-AC2C-4FEF-ABB5-C5349A359CC8}"
pptattach(1) = 0
'PDF attachments
ReDim pdfattach(1)
pdfattach(0) = "{707D20D7-5EF8-47D7-B6C8-47FCB606EEB5}"
pdfattach(1) = 15
'Audio Attachments
ReDim sndattach(1)
sndattach(0) = "{B28E76F5-127B-4356-9150-D2A0B84E8DCE}"
sndattach(1) = 18
'Video
ReDim vdoattach(1)
vdoattach(0) = "{E633EC9C-9B29-4608-A4BA-CFBFA886702B}"
vdoattach(1) = 23
'Image Attachment
ReDim imgAttach(1)
imgAttach(0) = "{BB488D85-76FE-408F-9DD4-617041DBFDA6}"
imgAttach(1) = 13
'Zip Attachment
ReDim zipAttach(1)
zipAttach(0) = "{B4423425-54F1-304F-92F3-63451D3BFDB6}"
zipAttach(1) = 8

Set catDict = CreateObject("Scripting.Dictionary")
catDict.add "Word Attachment",wdattah
catDict.add "Excel Attachment",edattach
catDict.add "PowerPoint Attachment", pptattach
catDict.add "PDF Attachment", pdfattach
catDict.add "Audio Attachment", sndattach
catDict.add "Image Attachment", imgAttach
catDict.add "Video Attachment", vdoattach
catDict.add "Zip Attachment", zipAttach

set xdXmlDocument = CreateObject("Microsoft.XMLDOM")
xdXmlDocument.async="false"
Set objSession = CreateObject("MAPI.Session")
objSession.Logon "","",false,true,true,true,snServername & vbLF & mbMailboxName
Set CdoInfoStore = objSession.GetInfoStore
Set CdoFolderRoot = CdoInfoStore.RootFolder
set cdocalendar = objSession.GetDefaultFolder(CdoDefaultFolderCalendar)
For Each soStorageItem in cdocalendar.HiddenMessages
If soStorageItem.Type = "IPM.Configuration.CategoryList" Then
hexString = soStorageItem.fields(&h7C080102).Value
xdXmlDocument.loadxml(hextotext(hexString))
For Each cat In catDict
catval = catDict(cat)
If SearchforCategory("name",cat,xdXmlDocument) = True Or SearchforCategory("guid",catval(0),xdXmlDocument) Then
wscript.echo "Category Name or GUID alread Exists " & cat
Else
wscript.echo "Adding category " & cat
Call AddCategory(cat,catDict(cat),xdXmlDocument)
End if
next
nval = StrToHexStr(CStr(xdXmlDocument.xml))
soStorageItem.fields(&h7C080102).Value = nval
soStorageItem.update
End if
Next



Function hextotext(binprop)
arrnum = len(binprop)/2
redim aout(arrnum)
slen = 1
for i = 1 to arrnum
if CLng("&H" & mid(binprop,slen,2)) <> 0 then
aOut(i) = chr(CLng("&H" & mid(binprop,slen,2)))
end if
slen = slen+2
next
hextotext = join(aOUt,"")
end Function


Function StrToHexStr(strText)
Dim i, strTemp
For i = 1 To Len(strText)
strTemp = strTemp & Right("0" & Hex(Asc(Mid(strText, i, 1))), 2)
Next
StrToHexStr = Trim(strTemp)
End Function

Function SearchforCategory(elElementName,cnvalue,XMLDoc)
Set xnNodes = XMLDoc.selectNodes("//*[@" & elElementName &amp; " = '" & cnvalue & "']")
If xnNodes.length = 0 Then
SearchforCategory = False
else
SearchforCategory = True
End if

End Function

Function AddCategory(cnCategoryName,setarray,XMLDoc)
Set xnNodes = XMLDoc.selectNodes("//categories")
Set xnCatNodes = XMLDoc.selectNodes("//category")
Set objnewCat = xnCatNodes(0).cloneNode(true)
objnewCat.setAttribute "name",cnCategoryName
objnewCat.setAttribute "guid",setarray(0)
objnewCat.setAttribute "keyboardShortcut", 0
objnewCat.setAttribute "color", setarray(1)
objnewCat.setAttribute "usageCount", 0
objnewCat.setAttribute "lastTimeUsedNotes","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastTimeUsedJournal","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastTimeUsedTasks","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastTimeUsedContacts","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastTimeUsedMail","1601-01-01T00:00:00.000"
objnewCat.setAttribute "lastSessionUsed","1601-01-01T00:00:00.000"
xnNodes(0).appendChild objnewCat
End Function


3 comments:

Anonymous said...

Glen,
Thanks for the code. Your grammar is rather poor, however.

craig said...

Great post. How would you go about deleting all the categories first if you wanted to maintain a master list?

Thanks!

Glen said...

You can just overwrite the existing property with your master List. You might want to have a look at http://www.infinitec.de/post/2008/05/Working-with-the-MasterCategoryList-Via-WebDAV.aspx Henning put a lot more time into this and come up with a library that can help you do this.

Cheers
Glen