Thursday, October 04, 2007

Adding Document Favorite links for Direct File Access in OWA 2007 via a script

Direct File access is a pretty cool feature of OWA 2007 although may not be the easiest thing for your average user to get their head around. Somebody asked a question about pre-populating the favorites for a user which is a pretty good idea (actually when you think about it you really want something that’s policy driven). Currently there is no really easy or supported way of doing this programmatically. The document links themselves are stored in a storage item with a messageclass of IPM.Configuration.Owa.DocumentLibraryFavorites in the associated folder collection of NON_IPM_Subtree root of a mailbox. On this storage item there is a binary mapi property 0x7C080102 and the links are stored in a XML document.

I decided to see if I could write a script that could open this storage item and then add some nodes into the already existing property or create the property if it didn’t exist. The format of the doclib node looks something like

<docLib uri="file://sername/directory" dn="DisplayName"
hn="servername" uf="2"/>

This isn’t documented anywhere so I can only hazard to guess at that these elements actually mean but logically I would think

Uri - Path to the directory or sharepoint server

Dn – The displayName in OWA

Hn – HostName ?

Uf – This property seems to get set to one of 3 values which affects the way the icon shows in OWA if you have added just a hostname then it gets set to 2, if you add a share mapping it gets set to 6 if you added a deep url mapping eg a couple of directorys deep it gets set to 34.

From what I’ve noticed the IPM.Configuration.Owa.DocumentLibraryFavorites storage item only gets created the first time the user click on the documents link in OWA so the item wouldn’t be there normally if the user has not logged onto OWA or has never clicked this link.

As I said this script is unsupported and pretty untested and should only be used in test/dev environment. Also make sure you know how to use a Mapi editor like Outlook Spy or mfcMapi worse case is you’ll stuff up the IPM.Configuration.Owa.DocumentLibraryFavorites object and need to delete it.

The script uses CDO 1.2 to use the script you need to configure the first 4 lines which reflect the doclib I just talked about,

shareDN = "temp"
ShareHostName = "servername"
Shareuf = "6"
ShareURI = "file://servername/temp"

and then the 6,7 with the servername and mailboxname of the mailbox you want to access.

I’ve put a download of the script here the code itself look like

shareDN = "temp"
ShareHostName = "servername"
Shareuf = "6"
ShareURI = "file://servername/temp"

snServername = "mailserver"
mbMailboxName = "mailbox"

Const PR_PARENT_ENTRYID = &H0E090102
set xdXmlDocument = CreateObject("Microsoft.XMLDOM")
xdXmlDocument.async="false"
ifound = false
Set objSession = CreateObject("MAPI.Session")
objSession.Logon "","",false,true,true,true,snServername & vbLF & mbMailboxName
Set CdoInfoStore = objSession.GetInfoStore
Set CdoFolderRoot = CdoInfoStore.RootFolder
set non_ipm_rootfolder =
objSession.getfolder(CdoFolderRoot.fields.item(PR_PARENT_ENTRYID),CdoInfoStore.id)
For Each soStorageItem in non_ipm_rootfolder.HiddenMessages
If soStorageItem.Type = "IPM.Configuration.Owa.DocumentLibraryFavorites" Then
ifound = true
Set actionItem = soStorageItem
End if
Next
If ifound = false Then
wscript.echo "No Storage Item Found"
Else
On Error Resume Next
hexString = actionItem.fields(&h7C080102).Value
If Err.number <> 0 Then
On Error goto 0
wscript.echo "Property not set"
actionItem.fields.Add &h7C080102, vbBlob
actionItem.fields(&h7C080102).Value = StrToHexStr("<docLibs></docLibs>")
actionItem.update
hexString = actionItem.fields(&h7C080102).Value
End If
On Error goto 0
wscript.echo hextotext(hexString)
xdXmlDocument.loadxml(hextotext(hexString))
Set xnNodes = xdXmlDocument.selectNodes("//docLibs")
update = false
Call Adddoclib(shareDN,ShareHostName,Shareuf,ShareURI,xnNodes)
If update = True Then
nval = StrToHexStr(CStr(xdXmlDocument.xml))
actionItem.fields(&h7C080102).Value = nval
actionItem.update
wscript.echo "Storage Object Updated"
Else
wscript.echo "No Updates Performed"
End If
End If



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 Searchfordoclib(elElementName,cnvalue,XMLDoc)
Set xnSearchNodes = XMLDoc.selectNodes("//*[@" & elElementName & " = '" &
cnvalue & "']")
If xnSearchNodes.length = 0 Then
Searchfordoclib = False
else
Searchfordoclib = True
End if

End Function

sub Adddoclib(dn,hn,uf,uri,xnNodes)
If Searchfordoclib("dn",dn,xdXmlDocument) = False then
Set objnewEle = xdXmlDocument.createElement("docLib")
objnewEle.setAttribute "uri",uri
objnewEle.setAttribute "dn",dn
objnewEle.setAttribute "hn", hn
objnewEle.setAttribute "uf", uf
xnNodes(0).appendchild objnewEle
update = true
Else
wscript.echo "Dn exists " & DN
End if
End sub

9 comments:

Eric said...

Is there any other way to do this without scripts? And what happens when you have more than one location?

Glen said...

Other then getting the user to add them manually not that i know off. (this a unsupported hack :( ).

Dont understand your second question you need to create a separate shortcut for each location.

Cheers
Glen

Anonymous said...

Thanks, Glen! This script and your your script to enable Junk Mail filtering have saved us many staff hours that would be used helping users set these things up.

Steve said...

is there a way to do this by sending a user a link in an email and have it create the OWA documents favorite to their account?

Glen said...

Hi Steve,

Not with a link but the customization features of OWA do offer a possible way to do this via sending a message with a custom message class. Then you register some code that runs whenever the user opens this type of message in OWA this could then modify the users setting. I'm pretty sure that would work but would be a complicated solution. To me it sounds like a cool solution one of those custom hacky solutions this blog is famous for if only i had the time.

Cheers
Glen

Anonymous said...

Is there a way to create the IPM.Configuration.Owa.DocumentLibraryFavorites storage item without having the user click on the Documents button in OWA? Thanks.

Glen said...

Yes you can but the format isn't documented for the properties you need to set so you need to try to reverse engineer using a Mapi editor like OutlookSpy or MFC mapi to make sure you set all the right Mapi properties.

Cheers
Glen

Glen said...

Actually the format is documented now have a look at http://download.microsoft.com/download/5/D/D/5DD33FDF-91F5-496D-9884-0A0B0EE698BB/%5BMS-OXOCFG%5D.pdf very cool :)

Cheers
Glen

Anonymous said...

hi Glen, i did the changes to the script in the server name and mailbox, but when i run it, it gives me an error: insertdoclink.vbs (13,1)Microsoft VBscript runtime error: ActiveX Component can´t create object: 'MAPI.Sesion', im runnig it on an exchange 2007 with windows 2008 64bits virtual machine, any ideas what the error is?? thanks.