Thursday, September 30, 2004

Scripting Contact folder Distribution List though OWA

Contact folder distribution lists presents some what of a challenge when it comes to scripting and programmatic access. The members in these lists are held in a couple of binary MAPI properties and there are no direct interfaces to modify these lists using CDOEX. Modifing the properties directly using Exoledb or WebDAV is possible in some cases just not very easy or flexible. Fortunately OWA does provide a method to create and modify these contact distribution lists which can easily be used in a automation script.

Creating a DL,

This is the one thing that you can do easily using CDOEX,ADO or WEBDAV but one extra thing you can do with OWA is create a DL and add a member to it at the same time. The one restriction I found for this is you can only add one member at a time per request. The following script creates a new DL in a public folder using the save cmd. You need to put the name of your new DL in http://schemas.microsoft.com/mapi/dlname=NewDlName

Set ObjxmlHttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlstr = ""
xmlstr = xmlstr & "Cmd=save" & vbLf
xmlstr = xmlstr & "msgclass=IPM.DistList" & vbLf
xmlstr = xmlstr & "http://schemas.microsoft.com/mapi/dlname=NewDlName"
ObjxmlHttp.Open "POST", "http://server/public/test2/", false, "domain\user", "pass"
ObjxmlHttp.setRequestHeader "Accept-Language", "en-us"
ObjxmlHttp.setRequestHeader "Content-type", "application/x-www-UTF8-encoded"
ObjxmlHttp.setRequestHeader "Content-Length", Len(xmlstr)
ObjxmlHttp.Send xmlstr

I used MSXML2.ServerXMLHTTP because of the responses OWA gives back it gets a little bit messy and it best just to ignore the responses.

Adding members to a DLL

To add a member to a existing DL you first need to know what the URL of DL is (be careful its usually more then just the displayname you can use something like exchange explorer to look at the dav:href of the object if your not sure). Then what you do is post to the DL name submitting some commands in the body of the post

Set ObjxmlHttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlstr = ""
xmlstr = xmlstr & "Cmd=addmember" & vbLf
xmlstr = xmlstr & "msgclass=IPM.DistList" & vbLf
xmlstr = xmlstr & "member=user@domain.com" & vbLf
ObjxmlHttp.Open "POST", "http://server/public/test2/Dlname.eml", false, "domain\user", "pass"
ObjxmlHttp.setRequestHeader "Accept-Language", "en-us"
ObjxmlHttp.setRequestHeader "Content-type", "application/x-www-UTF8-encoded"
ObjxmlHttp.setRequestHeader "Content-Length", Len(xmlstr)
ObjxmlHttp.Send xmlstr


Viewing the contents of a Distribution list

Viewing the members of a DL can be useful for a number of different purposes and its pretty easy all you need to do is issue the Cmd=viewmembers. What you get back is some XML with the email and memberid of each member in the DL. The MemberID comes in handy if you need to delete a member from the DL . Here's a script that demos this

Set ObjxmlHttp = CreateObject("Microsoft.XMLHTTP")
ObjxmlHttp.Open "GET","http://server/public/test2/dlname.EML?Cmd=viewmembers", False, "domain\user", "pass"
ObjxmlHttp.Send
set oResponseDoc = ObjxmlHttp.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("memberid")
set oNodeList1 = oResponseDoc.getElementsByTagName("email")
For i = 0 To (oNodeList.length -1)
set oNode = oNodeList.nextNode
set oNode1 = oNodeList1.nextNode
wscript.echo oNode.text & " " & oNode1.text
next


Deleteing a member from a DL

To delete a member from a DL you first need to know what the memberid of the member you want to delete is. So you need to combine a Cmd=viewmembers query with a delete function. The code below does a loop though all the members of the DL until it matches a email address its supplied and then calls the delete function.

usertodel = "user@domain.com"
Set ObjxmlHttp = CreateObject("Microsoft.XMLHTTP")
ObjxmlHttp.Open "GET","http://server/public/test2/dlname.EML?Cmd=viewmembers", False, "domain\user", "pass"
ObjxmlHttp.Send
set oResponseDoc = ObjxmlHttp.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("memberid")
set oNodeList1 = oResponseDoc.getElementsByTagName("email")
For i = 0 To (oNodeList.length -1)
set oNode = oNodeList.nextNode
set oNode1 = oNodeList1.nextNode
if oNode1.text = usertodel then
delmember(oNode.text)
wscript.echo "Member Deleted " & oNode1.text
end if
next

function delmember(utodel)
Set ObjxmlHttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlstr = ""
xmlstr = xmlstr & "Cmd=deletemember" & vbLf
xmlstr = xmlstr & "msgclass=IPM.DistList" & vbLf
xmlstr = xmlstr & "memberid=" & utodel
ObjxmlHttp.Open "POST", "http://server/public/test2/dlname.EML", false, "domain\user", "pass"
ObjxmlHttp.setRequestHeader "Accept-Language", "en-us"
ObjxmlHttp.setRequestHeader "Content-type", "application/x-www-UTF8-encoded"
ObjxmlHttp.setRequestHeader "Content-Length", Len(xmlstr)
ObjxmlHttp.Send xmlstr
end function


Some things you need to be careful off, I found you always need to specify the authentication as pass though didn't seem to work. I've put copies off all the scripts from this post here. http://msgdev.mvps.org/exdevblog/contactdl.zip

Thursday, September 23, 2004

Copying Contacts from One Mailbox to Another via Script

I had this request from one of my customers recently who wanted me to copy all the contacts from one mailbox into another. The easiest way to do this would have been to use outlook to export the contacts folder to a pst and then just import them back into the other mailbox using Outlook. Because i manage this client remotely and there's no Outlook on the Exchange server itself (and i really didn't feel like talking the users though this) I came up with the following Exoledb/CDOEX script to do the copy. The challenge when copying anything between mailbox's or public folders is you cant do a direct copy (eg ADO copyrecord only works within a mailbox or public store) so you have to use another solution.

What this script does is first does a query to get all the URL's of contacts in the mailbox's contact folder. Then it opens up each contact using the CDO.Person interface and then access's the vcard stream on each contact object . The script then copies this vcard stream into a new CDO.Person object and then saves it to the store which will then gernerate all the nessasary MAPI contact properties based on the information that is in the vCard Stream. The vCard stream has most of the information needed to create the contact as it was in the source mailbox but there are a few specific MAPI properties that are missing to be able to replicate this exactly. The first of these is the fileas property which ensures that when you recreate the contact they will be listed in the same order as they where in the source mailbox. The other import one that needs to be copied is the postal address, the business address gets copied in the vCard stream but the postal address doesn't and Outlook actually uses the postal address to display the address in the normal contact view . Some other things that don't copy over properly when you use the vCard stream is the email displayname and if you have a X400 address instead of a SMTP address this also get mangled so I've included code that copies each of those address properties manually. I've only included the properties for the first email contact field (if you want the 2nd and 3rd you'll have to get them yourself).

I've posted a copy of the script up here http://msgdev.mvps.org/exdevblog/copcont2.zip

here's what the code looks like

on error resume next
Set Rs = CreateObject("ADODB.Recordset")
set Rec = CreateObject("ADODB.Record")
set contobj = createobject("CDO.Person")
Set Conn = CreateObject("ADODB.Connection")
contactfolderurl = "file://./backofficestorage/domain.com/MBX/sourcemb/contacts/"
Conn.Provider = "ExOLEDB.DataSource"
Rec.Open contactfolderurl, ,3
SSql = "Select ""DAV:href"", ""http://schemas.microsoft.com/exchange/permanenturl"" "
SSql = SSql & " FROM scope('shallow traversal of """ & contactfolderurl & """') "
SSql = SSql & "WHERE ""DAV:contentclass"" = 'urn:content-classes:person' and ""DAV:ishidden"" = false"
Rs.CursorLocation = 3 'adUseServer = 2, adUseClient = 3
Rs.CursorType = 3
rs.open SSql, rec.ActiveConnection, 3
if Rs.recordcount <> 0 then
Rs.movefirst
while not rs.eof
set contobj1 = createobject("CDO.Person")
wscript.echo rs.fields("DAV:href")
ourl = rs.fields("http://schemas.microsoft.com/exchange/permanenturl")
contobj.datasource.open ourl,,3
set stm = contobj.getvcardstream()
set stm1 = contobj1.getvcardstream()
stm1.writetext = stm.readtext
stm1.flush
contobj1.fields("urn:schemas:contacts:fileas") = contobj.fields("urn:schemas:contacts:fileas")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x8080") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x8080")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818A") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818A")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818B") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818B")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818C") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818C")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818D") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818D")
contobj1.fields("http://schemas.microsoft.com/mapi/proptag/0x3A15001E") = contobj.fields("http://schemas.microsoft.com/mapi/proptag/0x3A15001E")
contobj1.fields("http://schemas.microsoft.com/mapi/proptag/0x3A2A001E") = contobj.fields("http://schemas.microsoft.com/mapi/proptag/0x3A2A001E")
contobj1.fields("http://schemas.microsoft.com/mapi/proptag/0x3A2B001E") = contobj.fields("http://schemas.microsoft.com/mapi/proptag/0x3A2B001E")
contobj1.fields.update
contobj1.datasource.savetocontainer "file://./backofficestorage/domain.com/MBX/targetmb/contacts/"
wscript.echo err.description
err.clear
set stm = nothing
set stm1 = nothing
set contobj1 = nothing
rs.movenext
wend
end if

Friday, September 10, 2004

Simple HTML calendar feed from Users Free/Busy data

A while ago I posted this about creating a simple html calendar like the ones you see in OWA. I was looking though some OWA captures today and noticed the following command is called when you manipulate the calender object in OWA.

?Cmd=monthfreebusy&start=2004-07-25T00:00:00+10:00&end=2004-09-05T00:00:00+10:00

(the +10:00 at the end I believe refers to the time zone offset which you need to adjust to your own timezone or put some code in that does it)

What this does is return a single string of numbers with each day in-between the &start and &end variables represented by a 0 if there are no appointments that day, a 1 if there is and a 2 is no freebusy data has been published.

I took this function and combined it with some WebDAV in a ASP page and was able to feed the simple html calendar and make it bold all the dates in the calendar that there where appointments on, I also created a few functions so you could change months back and forth.

The limitations of this method is that it only works for the months where free-busy information has been published and it doesn't work for public folder calendars because no free busy information is maintained for these. It should however come in handy for things like resource mailboxes where having a simple calendar comes in handy.

I've posted up a copy of the ASP page and button images here http://msgdev.mvps.org/exdevblog/showcalendar.zip (don't forget to change the timezone unless you live on the East coast of Aus)

The code itself look like


<table border="0" id="table1" cellpadding="2" width="147">
<tr><b>
<%
sdate = request.querystring("sdate")
if sdate = "" then
wdate = now()
mmonth = monthname(month(now())) & " " & Year(now())
else
wdate = dateserial(mid(sdate,1,4),mid(sdate,6,2),mid(sdate,9,2))
mmonth = monthname(month(wdate)) & " " & Year(wdate)
end if
pmonth = condate(dateadd("m",-1,wdate))
stime = condate(wdate)
etime = condate(dateadd("m",1,wdate))
response.write "<td style=""padding: 0"" width=""147"" align=""center"" colspan=""7""><b><font
face=""Arial Narrow"" size=2 color=""#000080"">"
response.write "<a href=""showcalendar.asp?sdate=" & etime & """><img
border=""0"" src=""pg-next.gif"" width=""16"" height=""16""
align=""right""></a>"
response.write "<a href=""showcalendar.asp?sdate=" & pmonth & """><img
border=""0"" src=""pg-prev.gif"" width=""16"" height=""16""
align=""left""></a><B>" & mmonth & "</b></td>"
%>
</font></b> </tr>
<tr>
<td style="border-bottom-style: solid; padding: 0" width="16" align="center">
M</td>
<td style="border-bottom-style: solid; padding: 0" width="16" align="center">
T</td>
<td style="border-bottom-style: solid; padding: 0" width="16" align="center">
W</td>
<td style="border-bottom-style: solid; padding: 0" width="16" align="center">
T</td>
<td style="border-bottom-style: solid; padding: 0" width="16" align="center">
F</td>
<td style="border-bottom-style: solid; padding: 0" width="16" align="center">
S</td>
<td style="border-bottom-style: solid; padding: 0" width="16" align="center">
S</td>
</tr>
<%

urlstr = "http://servername/exchange/mailbox/calendar/?Cmd=monthfreebusy&start="
& stime & "T00:00:00+10:00&end=" & etime & "T00:00:00+10:00"
Set Objxml = Server.CreateObject("Microsoft.XMLhttp")
Objxml.Open "Get", urlstr, False, "", ""
Objxml.setRequestHeader "Accept-Language:", "en-us"
Objxml.setRequestHeader "Content-type:", "application/x-www-UTF8-encoded"
Objxml.setRequestHeader "Content-Length:", Len(szXml)
Objxml.Send szXml
set oResponseDoc = Objxml.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("fbdata")
For i = 0 To (oNodeList.length -1)
set oNode = oNodeList.nextNode
caldata = oNode.text
next
cdatefday = cdate(dateserial(year(wdate),month(wdate),"1"))
sday = weekday(cdatefday,2)
cmonth = month(wdate)
for x = 1 to 6
response.write "<tr>"
for i = 1 to 7
if cmonth = month(cdatefday) then
if sday =< i then
sday = 0
if mid(caldata,day(cdatefday),1) = 1 then
response.write "<td style=""padding: 0"" width=""16"" align=""center""><b>" &
day(cdatefday) & "</b></td>"
else
response.write "<td style=""padding: 0"" width=""16"" align=""center"">" &
day(cdatefday) & "</td>"
end if
cdatefday = dateadd("d",1,cdatefday)
else
response.write "<td style=""padding: 0"" width=""16"" align=""center""> </td>"

end if
else
response.write "<td style=""padding: 0"" width=""16"" align=""center""> </td>"

end if
next
response.write "</tr>"
next


function condate(date2con)
dtcon = date2con
if month(dtcon) < 10 then
if day(dtcon) < 10 then
qdat = year(dtcon) & "-" & "0" & month(dtcon) & "-" & "01"
else
qdat = year(dtcon) & "-" & "0" & month(dtcon) & "-" & "01"
end if
else
if day(dtcon) < 10 then
qdat = year(dtcon) & "-" & month(dtcon) & "-" & "01"
else
qdat = year(dtcon) & "-" & month(dtcon) & "-" & "01"
end if
end if
condate = qdat
end function
%>

Wednesday, September 08, 2004

Public folder RSS Feed Event sink v2

From the feedback I've received about the first public folder RSS feed sink I've come up with another version that incorporates many bug fixes and add's in some additional functionality.

The main functionality change and bug fix revolves around the publishing of the content of each public folder message. In the original sink I was using the Textdescription field which represented the text body of the email. The problem with this is that firstly the carriage returns weren't interpreted right in the XML file so if you had a long message it just appeared all stuck together with no formatting. To fix this there where two options the first was to continue using the textdescription field and then just replace any ASCII 13 characters (Line breaks in a email) with html line break characters eg

replace(Rs.fields("urn:schemas:httpmail:textdescription"),chr(13),"<br/>")

The second option and the one I've gone for is to use the urn:schemas:httpmail:htmldescription this allows for all the formatting to be retained in the XML file and this displays okay in various RSS feed readers I've tested.

The other functionality added was to change the link section so instead of linking back to the eml file that would take you to OWA if you clicked the link now it will redirect to a ASP file. This ASP file takes in two html querystrings passed in from the link in the RSS Feed and then does a webdav get on the feedfile and returns a webpage representation of that RSS feed entry. This is pretty cool because what it allows you to do is you can publish the feed file and asp file on a external web server and then allow people to access the contents of your Exchange public folders without even needing them to access your Exchange box via OWA.

To cater for the link functionality I've added a GUID node into each item entry based on the DAV:getetag field of each message. (Its possible I may regret using this field as im not 100% sure what its actually for but it was a nice unique number with no wacky characters that i could pass around, other things like Dav:href while unique i found caused problems in certain situations).

Other bug fixes include the RFC822 date fix, I've tried to get rid of all the hardcoded servernames and used two variables that are set at the top of the script instead for the webservername and feedfile name. Some small formatting changes where also made to make it more compatible across multiple RSS feed aggregates.

I recently got some webspace to post the scripts I put up on this blog so I've uploaded the complete code for the eventsink and the showmessage.asp page and they can be downloaded from http://msgdev.mvps.org/exdevblog/pubfeedv2.zip

To use the showmessage.asp page this needs to be located in the same directory as the feed file (if not you'll need to modify the code). I hope for the next version to do a complied version with a installer so you won't need to know anything about event sinks to use it. Here's the code for the new sink and showmessage.asp.


Sub ExStoreEvents_OnSave(pEventInfo, bstrURLItem, lFlags)

on error resume next
WebServer = "Intranet"
feedfile = "feedpub2.xml"
set DispEvtInfo = pEventInfo
set ADODBRec = DispEvtInfo.EventRecord
set objdom = CreateObject("MICROSOFT.XMLDOM")
Set objField = objDom.createElement("rss")
Set objattID = objDom.createAttribute("version")
objattID.Text = "2.0"
objField.setAttributeNode objattID
objDom.appendChild objField
Set objField1 = objDom.createElement("channel")
objfield.appendChild objField1
Set objField3 = objDom.createElement("link")
objfield3.text = "http://" & WebServer & "/showmessage.asp?xmlfile=" & feedfile & "&message=All"
objfield1.appendChild objField3
Set objField4 = objDom.createElement("title")
objfield4.text = "Public Folder Feed"
objfield1.appendChild objField4
Set objField5 = objDom.createElement("description")
objfield5.text = "Public Folder Feed For Path"
objfield1.appendChild objField5
Set objField6 = objDom.createElement("language")
objfield6.text = "en-us"
objfield1.appendChild objField6
Set objField7 = objDom.createElement("lastBuildDate")
objfield7.text = WeekdayName(weekday(now),3) & ", " & day(now()) & " " & Monthname(month(now()),3) & " " & year(now()) & " " & formatdatetime(now(),4) & ":00 GMT"
objfield1.appendChild objField7
Set Rs = CreateObject("ADODB.Recordset")
Set fso = CreateObject("Scripting.FileSystemObject")
Set msgobj = CreateObject("CDO.Message")
tyear = year(now()-7)
tmonth = month(now()-7)
if tmonth < 10 then tmonth = 0 & tmonth
stday = day(now()-7)
if stday < 10 then stday = 0 & stday
sttime = formatdatetime(now1,4)
qdatest = tyear & "-" & tmonth & "-" & stday & "T"
qdatest1 = qdatest & sttime & ":" & "00Z"
set Rec = CreateObject("ADODB.Record")
set Rec1 = CreateObject("ADODB.Record")
Set Conn = CreateObject("ADODB.Connection")
mailboxurl = ADODBRec.fields("Dav:parentname")
Conn.Provider = "ExOLEDB.DataSource"
Rec.Open mailboxurl, ,3
SSql = "SELECT ""DAV:href"", ""DAV:getetag"", ""DAV:contentclass"", ""urn:schemas:httpmail:htmldescription"", ""urn:schemas:httpmail:datereceived"", "
SSql = SSql & """urn:schemas:httpmail:fromemail"", ""urn:schemas:httpmail:subject"", ""DAV:ishidden"" "
Ssql = SSql & " FROM scope('shallow traversal of """ & mailboxurl & """') "
SSql = SSql & " WHERE (""urn:schemas:httpmail:datereceived"" > CAST(""" & qdatest1 & """ as 'dateTime')) AND ""DAV:isfolder"" = false"
Rs.CursorLocation = 3 'adUseServer = 2, adUseClient = 3
Rs.CursorType = 3
rs.open SSql, rec.ActiveConnection, 3
if Rs.recordcount <> 0 then
Rs.movefirst
while not rs.eof
if rs.fields("DAV:ishidden") = 0 then
Set objField2 = objDom.createElement("item")
objfield1.appendChild objField2
Set objField8 = objDom.createElement("guid")
Set objattID8 = objDom.createAttribute("isPermaLink")
objattID8.Text = "false"
objField8.setAttributeNode objattID8
objfield8.text = replace(Rs.fields("DAV:getetag"),chr(34),"")
objfield2.appendChild objField8
Set objField9 = objDom.createElement("title")
objfield9.text = rs.fields("urn:schemas:httpmail:subject")
objfield2.appendChild objField9
Set objField10 = objDom.createElement("link")
objfield10.text = "http://" & WebServer & "/showmessage.asp?xmlfile=" & feedfile & "&message=" & replace(Rs.fields("DAV:getetag"),chr(34),"")
objfield2.appendChild objField10
Set objField11 = objDom.createElement("description")
objfield11.text = Rs.fields("urn:schemas:httpmail:htmldescription")
if objfield11.text = "" then objfield11.text = "Blank"
objfield2.appendChild objField11
Set objField12 = objDom.createElement("author")
objfield12.text = rs.fields("urn:schemas:httpmail:fromemail")
objfield2.appendChild objField12
Set objField13 = objDom.createElement("pubDate")
objfield13.text = WeekdayName(weekday(rs.fields("urn:schemas:httpmail:datereceived")),3) & ", " & day(rs.fields("urn:schemas:httpmail:datereceived")) & " " & Monthname(month(rs.fields("urn:schemas:httpmail:datereceived")),3) & " " & year(rs.fields("urn:schemas:httpmail:datereceived")) & " " & formatdatetime(rs.fields("urn:schemas:httpmail:datereceived"),4) & ":00 GMT"
objfield2.appendChild objField13
set objfield2 = nothing
set objfield8 = nothing
set objfield9 = nothing
set objfield10 = nothing
set objfield11 = nothing
set objfield12 = nothing
set objfield13 = nothing
end if
rs.movenext
wend
end if
rs.close
Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")
objDom.insertBefore objPI, objDom.childNodes(0)
objdom.save("\\" & Webserver & "\wwwroot\" & feedfile)

End Sub

showmessage.asp file


<%
on error resume next
set xmlfile1 = request.querystring("xmlfile")
xmlfile = "http://" & Request.ServerVariables("SERVER_NAME") &
Request.ServerVariables("URL")
xmlfile = left(xmlfile,(instr(xmlfile,"showmessage.asp")-1)) & xmlfile1
uid = request.querystring("message")
set xmlobj = server.createobject("microsoft.xmlhttp")
xmlobj.Open "Get", xmlfile, False, "", ""
xmlobj.setRequestHeader "Accept-Language:", "en-us"
xmlobj.setRequestHeader "Content-type:", "text/xml"
xmlobj.Send
set oResponseDoc = xmlobj.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("*")
For i = 0 To (oNodeList.length -2)
set oNode = oNodeList.nextNode
if oNode.Text = uid then
guid = oNode.Text
set oNode = oNodeList.nextNode
Title = oNode.Text
set oNode = oNodeList.nextNode
Link = oNode.Text
set oNode = oNodeList.nextNode
Description = oNode.Text
set oNode = oNodeList.nextNode
Author = oNode.Text
set oNode = oNodeList.nextNode
pubdate = oNode.Text
end if
Next
pubdate = replace(pubdate,"GMT","")
pubdate = cdate(Mid(pubdate,(instr(pubdate,",")+1),len(pubdate)))
set shell = server.createobject("wscript.shell")
strValueName = "HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias"
minTimeOffset = shell.regread(strValueName)
toffset = datediff("h",DateAdd("n", minTimeOffset, now()),now())
pubdate = dateadd("h",toffset,pubdate)


%><table border="1" cellspacing="1" width="100%" id="table1">
<tr>
<td>Received: <%=pubdate%></td>
</tr>
<tr>
<td>From: <%=author%></td>
</tr>
<tr>
<td>Subject: <%=Title%></td>
</tr>
<tr>
<td>Message: <%=Description%></td>
</tr>
</table>

Thursday, September 02, 2004

Processing Meeting requests remotely with WebDAV and OWA

This was something interesting I learned this week. If you want to process meeting requests remotely the same way you can locally with CDOEX then using a combination of WebDAV and some OWA commands can make this happen.

Process the request

Basically when someone invites you to a meeting you will get sent a calendar message that contains all the invitation information. You can differentiate this from a normal message by looking at the DAV:Contentclass and seeing if its set to urn:content-classes:calendarmessage. Now if you want to scan a inbox for any of these messages you could do a WebDAV search against that mailbox for any messages that have this particular content class and also who’s outlookmessage class was set to IPM.Schedule.Meeting.Request which delineates this as a meeting request. Eg

server = "servername"
mailbox = "mailbox"
strURL = "http://" & server & "/exchange/" & mailbox & "/inbox/"
strQuery = ""
strQuery = strQuery & "SELECT ""DAV:href"", ""http://schemas.microsoft.com/exchange/outlookmessageclass"""
strQuery = strQuery & " FROM scope('shallow traversal of """
strQuery = strQuery & strURL & """') Where ""DAV:ishidden"" = False AND ""DAV:isfolder"" = False AND "
strQuery = strQuery & """DAV:contentclass"" = 'urn:content-classes:calendarmessage'
"
set req = createobject("microsoft.xmlhttp")
req.open "SEARCH", strURL, false
req.setrequestheader "Content-Type", "text/xml"
req.setRequestHeader "Translate","f"
req.send strQuery
If req.status >= 500 Then
wscript.echo "Status: " & req.status & " Status text: An error occurred on the server."
ElseIf req.status = 207 Then
wscript.echo "Status: " & req.status & " Status text: An error occurred on the server."
set oResponseDoc = req.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("a:href")
set oclasslist = oResponseDoc.getElementsByTagName("d:outlookmessageclass")
For i = 0 To (oclasslist.length -2)
set oNode1 = oclasslist.nextNode
set oNode = oNodeList.nextNode
if oNode1.Text = "IPM.Schedule.Meeting.Request" then acceptcalmess(oNode.Text)
Next
Else
wscript.echo "Status: " & req.status & " Status text: An error occurred on the server."
End If

sub acceptcalmess(objhref)
wscript.echo objhref
end sub

Now to process the calendar messages this is where you can use the OWA accept and decline commands. Once you issue one of these commands OWA will create a new calendar response message saying that you have accepted or declined this meeting and save this into your drafts folder (the original calendar request message is also deleted). To send this message all you then need to do is move it to the DavMailSubmissionURI. To put this all together with the search example it would look like this

server = "servername"
mailbox = "mailbox"
strURL = "http://" & server & "/exchange/" & mailbox & "/inbox/"
strQuery = ""
strQuery = strQuery & "SELECT ""DAV:href"", ""http://schemas.microsoft.com/exchange/outlookmessageclass"""
strQuery = strQuery & " FROM scope('shallow traversal of """
strQuery = strQuery & strURL & """') Where ""DAV:ishidden"" = False AND ""DAV:isfolder"" = False AND "
strQuery = strQuery & """DAV:contentclass"" = 'urn:content-classes:calendarmessage'
"
set req = createobject("microsoft.xmlhttp")
req.open "SEARCH", strURL, false
req.setrequestheader "Content-Type", "text/xml"
req.setRequestHeader "Translate","f"
req.send strQuery
If req.status >= 500 Then
wscript.echo "Status: " & req.status
wscript.echo "Status text: An error occurred on the server."
ElseIf req.status = 207 Then
wscript.echo "Status: " & req.status
wscript.echo "Status text: " & req.statustext
set oResponseDoc = req.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("a:href")
set oclasslist = oResponseDoc.getElementsByTagName("d:outlookmessageclass")
For i = 0 To (oclasslist.length -2)
set oNode1 = oclasslist.nextNode
set oNode = oNodeList.nextNode
if oNode1.Text = "IPM.Schedule.Meeting.Request" then acceptcalmess(oNode.Text)
Next
Else
wscript.echo "Status: " & req.status
wscript.echo "Status text: " & req.statustext
wscript.echo "Response text: " & req.responsetext
End If

sub acceptcalmess(objhref)

Req.open "GET", objhref & "?cmd=Accept", false
Req.send
wscript.echo req.status
sDestinationURL = "http://" & server & "/exchange/" & mailbox & "/##DavMailSubmissionURI##/"
sSource = replace(lcase(objhref),"/inbox/","/drafts/")
req.open "MOVE", sSource, False
req.setRequestHeader "Destination", sDestinationURL
req.setRequestHeader "Content-Type", "message/rfc822;"
req.setRequestHeader "Translate", "f"
req.setRequestHeader "Content-Length:", Len(xmlstr)
req.send(xmlstr)
wscript.echo req.status
end sub

The last thing you might want to do in regards to processing calendar messages is to process that actual responses that come back from people so that the attendee status in the appointment is updated with accepted, rejected and any new attendees that are invited. To do this all you need to do is open the responses with the open cmd from OWA and OWA will process the request and update the meeting appropriately. To identify the response calendar messages from a request calendar messages you can use the Oulookmessageclass and see if its of type IPM.Schedule.Meeting.Resp.Pos, IPM.Schedule.Meeting.Resp.Neg, IPM.Schedule.Meeting.Resp.Tent .

server = "servername"
mailbox = "username"
strURL = "http://" & server & "/exchange/" & mailbox & "/inbox/"
strQuery = ""
strQuery = strQuery & "SELECT ""DAV:href"", ""http://schemas.microsoft.com/exchange/outlookmessageclass"""
strQuery = strQuery & " FROM scope('shallow traversal of """
strQuery = strQuery & strURL & """') Where ""DAV:ishidden"" = False AND ""DAV:isfolder"" = False AND "
strQuery = strQuery & """DAV:contentclass"" = 'urn:content-classes:calendarmessage'
"
set req = createobject("microsoft.xmlhttp")
req.open "SEARCH", strURL, false
req.setrequestheader "Content-Type", "text/xml"
req.setRequestHeader "Translate","f"
req.send strQuery
If req.status >= 500 Then
wscript.echo "Status: " & req.status
wscript.echo "Status text: An error occurred on the server."
ElseIf req.status = 207 Then
wscript.echo "Status: " & req.status
wscript.echo "Status text: " & req.statustext
set oResponseDoc = req.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("a:href")
set oclasslist = oResponseDoc.getElementsByTagName("d:outlookmessageclass")
For i = 0 To (oclasslist.length -2)
set oNode1 = oclasslist.nextNode
set oNode = oNodeList.nextNode
select case oNode1.Text
case "IPM.Schedule.Meeting.Resp.Neg" acceptcalmess(oNode.Text)
case "IPM.Schedule.Meeting.Resp.Tent" acceptcalmess(oNode.Text)
case "IPM.Schedule.Meeting.Resp.Pos" acceptcalmess(oNode.Text)
end select
Next
Else
wscript.echo "Status: " & req.status
wscript.echo "Status text: " & req.statustext
wscript.echo "Response text: " & req.responsetext
End If

sub acceptcalmess(objhref)

Req.open "GET", objhref & "?cmd=Open", false
Req.send
wscript.echo req.status
end sub



Note processing the meeting responses this way does not delete the response you have to do this manually.

What about task requests?
Task requests seem to be a little different OWA for example doesn’t give you the option to process task requests. So this method is really not applicable.