Wednesday, June 23, 2004

Feeding the simple Html Calendar with Exoledb

Man this was a lot harder then I expected it would be, When it comes to representing time in anything you are really starting to deal with a different dimension (I think time is the third dimension). Eg you can select all the records in a calendar with the startdate greater then the first of the month and a startdate less then the end of the month but this is going to miss all multi-day appointments. Also in my example say I wanted to know if there where any appointments that where on the 5th then just looking at the start date of an appointment wouldn't necessarily tell me this because I could have a multiday event that started on the 4th and finished on the 6th. The other problem i faced is i wanted to show all the days where there was some appointments scheduled as bold and clickable trying to do this you also run into the same problem where appointments span Multiple days (Also a lot of advanced aggregate functions aren't available in Exoledb).

To solve this problem my solution was to use a sliding time window, so to get all the appointments for the month I had to query for all appointments with a enddate greater then the 1st of the month and a startdate that was less then the 1st day of next month. To work out if there were any appointments on each particular day I did a 31 repetition for next loop using a time window that spanned a 1 day and re-filtered the recordset for each repitition. (the logic sounds a bit weird but its one of those things you really need to think about but it does work and only requires one query of the Exchange Server). Then I just reused the array in the for next loop that generated the calendar

To feed my Asp calendar I packaged up the Exoledb code into a WSC com object and then created a wrapper that runs under an account that had rights to the calendar I wanted this page to access. I also created some click thoughs that would display the days appointments in a table on the right of the page. This still needs a fair bit of work but it works fine at the moment. I would have prefered to maybe use some inline xml so i didn't have to requery Exchange everytime i looked at a different day.

WSC object ..


<?xml version="1.0"?>
<component>

<registration
description="calfeed"
progid="calfeed.WSC"
version="1.00"
classid="{4414f884-d473-401a-8357-b899f3de8f31}"
>
</registration>

<public>
<method name="mailbox">
<PARAMETER name="MBname"/>
</method>
</public>

<implements type="ASP" id="ASP"/>

<script language="VBScript">
<![CDATA[

function mailbox(MBname)

set shell = createobject("wscript.shell")
strValueName = "HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias"
minTimeOffset = shell.regread(strValueName)
toffset = datediff("h",DateAdd("n", minTimeOffset, now()),now())
dtListFrom = DateAdd("n", minTimeOffset, now())
gmttime = dateadd("h",toffset,now())
dtListTo = isodateit(dateserial(year(now()),month(dateadd("m",1,now())),1)& " " & timeserial(0,0,0))
dtListFrom = isodateit(dateserial(year(now()),month(now()),1) & " " & timeserial(0,0,0))
Set Rs = CreateObject("ADODB.Recordset")
set Rec = CreateObject("ADODB.Record")
Set Conn = CreateObject("ADODB.Connection")
CalendarURL = "http://yourserver/exchange/" & MBname & "/calendar"
Conn.Provider = "ExOLEDB.DataSource"
Rec.Open CalendarURL
Set Rs.ActiveConnection = Rec.ActiveConnection
Rs.Source = "SELECT ""DAV:href"", " & _
" ""urn:schemas:httpmail:subject"", " & _
" ""urn:schemas:calendar:dtstart"", " & _
" ""urn:schemas:calendar:dtend"", " & _
" ""urn:schemas:calendar:organizer"", " & _
" ""urn:schemas:calendar:location"", " & _
" ""DAV:contentclass"" " & _
"FROM scope('shallow traversal of """ & CalendarURL & """') " & _
"WHERE (""urn:schemas:calendar:dtend"" > CAST(""" & dtListFrom & """ as 'dateTime'))" & _
"AND (""urn:schemas:calendar:dtstart"" < CAST(""" & dtListTo & """ as 'dateTime'))"& _
" AND ""DAV:contentclass"" = 'urn:content-classes:appointment'" & _
"ORDER BY ""urn:schemas:calendar:dtstart"" ASC"

Rs.CursorLocation = 3 'adUseServer = 2, adUseClient = 3
Rs.CursorType = 3
Rs.Open
set mailbox = Rs
end function

function isodateit(datetocon)
strDateTime = year(datetocon) & "-"
if (Month(datetocon) < 10) then strDateTime = strDateTime & "0"
strDateTime = strDateTime & Month(datetocon) & "-"
if (Day(datetocon) < 10) then strDateTime = strDateTime & "0"
strDateTime = strDateTime & Day(datetocon) & "T" & formatdatetime(datetocon,4) &":00Z"
isodateit = strDateTime
end function


]]>
</script>

Calendar ASP page ....
<%
CD = request.querystring("cday")
set cfeed = createobject("calfeed.WSC")
set rs = cfeed.mailbox("yourmailbox")
set shell = createobject("wscript.shell")
strValueName = "HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias"
minTimeOffset = shell.regread(strValueName)
toffset = datediff("h",DateAdd("n", minTimeOffset, now()),now())
dtListFrom = DateAdd("n", minTimeOffset, now())
redim myarray(3,31)
for i = 1 to 31
rs.filter = "urn:schemas:calendar:dtend > '" & dateadd("h",-toffset,dateserial(year(now()),month(now()),i))
& "' and urn:schemas:calendar:dtstart < '" & dateadd("h",-toffset,dateserial(year(now()),month(now()),i+1)) & "'"
myarray(1,i) = rs.recordcount
myarray(2,i) = dateadd("h",-toffset,dateserial(year(now()),month(now()),i))
myarray(3,i) = dateadd("h",-toffset,dateserial(year(now()),month(now()),i+1))
next
rs.filter = ""
%>
<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>

<br>
<table border="1" cellspacing="1" width="100%">
<tr>
<td width="151"><table border="0" id="table1" cellpadding="2" width="147">
<tr><b><% response.write "<td style=""padding: 0"" width=""112""
align=""center"" colspan=""7""><b><font face=""Arial"" color=""#000080"">" &
monthname(month(now())) & " " & Year(now()) &"</td>"
%></font></b> </tr>
<tr>
<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">
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>
</tr>
<%
cdatefday = cdate(dateserial(year(now()),month(now()),"1"))
sday = weekday(cdatefday)
cmonth = month(now())
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 myarray(1,day(cdatefday)) = 0 then
response.write "<td style=""padding: 0"" width=""16"" align=""center"">" &
day(cdatefday) & "</td>"
else
response.write "<td style=""padding: 0"" width=""16"" align=""center""><a
href=""caltest2.asp?cday=" & day(cdatefday) & """><b>" & day(cdatefday) & "</a></b></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

%></td>
<td>&nbsp;</td>
</tr>
</table>
<td width="617"><table border="0" cellspacing="0" width="100%" id="table2" cellpadding="0"><tr>
<td width="154"><B>Start Date</B></td>
<td width="154"><B>End Date</B></td>
<td><B>Subject</B></td>
</tr>
<% If CD <> "" then
rs.filter = "urn:schemas:calendar:dtend > '" &
dateadd("h",-toffset,dateserial(year(now()),month(now()),CD)) & "' and urn:schemas:calendar:dtstart < '" & dateadd("h",-toffset,dateserial(year(now()),month(now()),CD+1)) & "'"
end if
while not rs.eof
response.write "<tr>" & vbcrlf
response.write "<td width=""154"">" & dateadd("h",toffset,rs.fields("urn:schemas:calendar:dtstart")) & "</td>" & vbcrlf
response.write "<td width=""154"">" &
dateadd("h",toffset,rs.fields("urn:schemas:calendar:dtend")) & "</td>" & vbcrlf
response.write "<td>" & rs.fields("urn:schemas:httpmail:subject") & "</td>" & vbcrlf
response.write "</tr>" & vbcrlf
rs.movenext
wend
%>
</table>



2 comments:

Anonymous said...

So how does one implement this on an IIS server?

Glen said...

Exoledb only works locally on a Exchange server. If you want to do this remotely you could look at using WebDAV see http://msdn2.microsoft.com/en-us/library/aa123570.aspx

Cheers
Glen