Pulling allday events from the public calendar of an exchange 2003 server vb.net webdav
This is code that pulls all day events from an exchange 2003 server using webdav.
1 2 Dim Request As System.Net.HttpWebRequest 3 Dim Response As System.Net.HttpWebResponse 4 Dim MyCredentialCache As System.Net.CredentialCache 5 Dim strPassword As String 6 Dim strDomain As String 7 Dim strUserName As String 8 Dim strCalendarURI As String 9 Dim strQuery As String 10 Dim bytes() As Byte 11 Dim RequestStream As System.IO.Stream 12 Dim ResponseStream As System.IO.Stream 13 Dim ResponseXmlDoc As System.Xml.XmlDocument 14 Dim HrefNodes As System.Xml.XmlNodeList 15 Dim SizeNodes As System.Xml.XmlNodeList 16 Dim SubjectNodeList As System.Xml.XmlNodeList 17 Dim LocationNodeList As System.Xml.XmlNodeList 18 Dim StartTimeNodeList As System.Xml.XmlNodeList 19 Dim EndTimeNodeList As System.Xml.XmlNodeList 20 Dim BusyStatusNodeList As System.Xml.XmlNodeList 21 Dim InstanceTypeNodeList As System.Xml.XmlNodeList 22 Dim output As String = "" 23 24 Try 25 ' Initialize variables. 26 strUserName = "user" 27 strPassword = "password" 28 strDomain = "domain" 29 30 strCalendarURI = "http://mail-server/public/Office Calendar/" 31 32 Dim ddate As DateTime = Today 33 34 Dim sStartTime As String = String.Format("{0:yyyy/MM/dd}", ddate) & " 07:00:00' " 35 Dim sEndTime As String = String.Format("{0:yyyy/MM/dd}", ddate.AddDays(1)) & " 07:00:00' " 36 37 strQuery = "<?xml version=""1.0""?>" & _ 38 "<g:searchrequest xmlns:g=""DAV:"">" & _ 39 "<g:sql>SELECT ""urn:schemas:calendar:alldayevent"", ""urn:schemas:calendar:location"", ""urn:schemas:httpmail:subject"", " & _ 40 """urn:schemas:calendar:dtstart"", ""urn:schemas:calendar:dtend"", " & _ 41 """urn:schemas:calendar:busystatus"", ""urn:schemas:calendar:instancetype"" " & _ 42 "FROM Scope('SHALLOW TRAVERSAL OF """ & strCalendarURI & """') " & _ 43 "WHERE ""DAV:contentclass"" = 'urn:content-classes:appointment' " & _ 44 "AND ""urn:schemas:calendar:alldayevent"" = TRUE " & _ 45 "AND ""urn:schemas:calendar:dtstart"" <= '" & sStartTime & _ 46 "AND ""urn:schemas:calendar:dtend"" >= '" & sEndTime & _ 47 "ORDER BY ""urn:schemas:calendar:dtstart"" ASC" & _ 48 "</g:sql></g:searchrequest>" 49 50 51 52 MyCredentialCache = New System.Net.CredentialCache 53 MyCredentialCache.Add(New System.Uri(strCalendarURI), _ 54 "NTLM", _ 55 New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _ 56 ) 57 58 ' Create the PUT HttpWebRequest object. 59 Request = CType(System.Net.WebRequest.Create(strCalendarURI), _ 60 System.Net.HttpWebRequest) 61 62 ' Add the network credentials to the request. 63 Request.Credentials = MyCredentialCache 64 65 ' Specify the SEARCH method. 66 Request.Method = "SEARCH" 67 68 ' Encode the body using UTF-8. 69 bytes = System.Text.Encoding.UTF8.GetBytes(strQuery) 70 71 ' Set the content header length. This must be 72 ' done before writing data to the request stream. 73 Request.ContentLength = bytes.Length 74 75 ' Get a reference to the request stream. 76 RequestStream = Request.GetRequestStream() 77 78 ' Write the message body to the request stream. 79 RequestStream.Write(bytes, 0, bytes.Length) 80 81 ' Close the Stream object to release the connection 82 ' for further use. 83 RequestStream.Close() 84 85 ' Set the Content Type header. 86 Request.ContentType = "text/xml" 87 88 ' Set the Translate header. 89 Request.Headers.Add("Translate", "F") 90 91 ' Send the SEARCH method request and get the 92 ' response from the server. 93 Response = CType(Request.GetResponse(), System.Net.HttpWebResponse) 94 95 ' Get the XML response stream. 96 ResponseStream = Response.GetResponseStream() 97 98 ' Create the XmlDocument object from the XML response stream. 99 ResponseXmlDoc = New System.Xml.XmlDocument 100 ResponseXmlDoc.Load(ResponseStream) 101 102 ' Build a list of the DAV:href XML nodes, corresponding to the folders 103 ' in the mailbox. The DAV: namespace is typically assgigned the a: 104 ' prefix in the XML response body. 105 HrefNodes = ResponseXmlDoc.GetElementsByTagName("a:href") 106 107 ' Build a list of the urn:schemas:httpmail:subject XML nodes, 108 ' corresponding to the calendar item subjects returned in the search request. 109 ' The urn:schemas:httpmail: namespace is typically 110 ' assigned the e: prefix in the XML response body. 111 SubjectNodeList = ResponseXmlDoc.GetElementsByTagName("e:subject") 112 113 ' Build a list of the urn:schemas:calendar:location XML nodes, 114 ' corresponding to the calendar item locations returned in the search request. 115 ' The urn:schemas:calendar: namespace is typically 116 ' assigned the d: prefix in the XML response body. 117 LocationNodeList = ResponseXmlDoc.GetElementsByTagName("d:location") 118 119 ' Build a list of the urn:schemas:calendar:dtstart XML nodes, 120 ' corresponding to the calendar item locations returned in the search request. 121 StartTimeNodeList = ResponseXmlDoc.GetElementsByTagName("d:dtstart") 122 123 ' Build a list of the urn:schemas:calendar:dtend XML nodes, 124 ' corresponding to the calendar item locations returned in the search request. 125 EndTimeNodeList = ResponseXmlDoc.GetElementsByTagName("d:dtend") 126 127 ' Build a list of the urn:schemas:calendar:busystatus XML nodes, 128 ' corresponding to the calendar item locations returned in the search request. 129 BusyStatusNodeList = ResponseXmlDoc.GetElementsByTagName("d:busystatus") 130 131 ' Build a list of the urn:schemas:calendar:instancetype XML nodes, 132 ' corresponding to the calendar item locations returned in the search request. 133 InstanceTypeNodeList = ResponseXmlDoc.GetElementsByTagName("d:instancetype") 134 135 ' Loop through the returned items (if any). 136 If SubjectNodeList.Count > 0 Then 137 138 Dim i As Integer 139 For i = 0 To SubjectNodeList.Count - 1 140 141 ' Display the subject. 142 143 output = output & SubjectNodeList(i).InnerText & ControlChars.CrLf 144 Console.WriteLine(" " + SubjectNodeList(i).InnerText) 145 146 ' Display the location. 147 'Console.WriteLine(" Location: " + LocationNodeList(i).InnerText) 148 149 ' Display the start time. 150 'Console.WriteLine(" Start time: " + StartTimeNodeList(i).InnerText) 151 152 ' Display the end time. 153 'Console.WriteLine(" End time: " + EndTimeNodeList(i).InnerText) 154 155 ' Display the busy status. 156 'Console.WriteLine(" Busy status: " + BusyStatusNodeList(i).InnerText) 157 158 ' Display the instance type. 159 'If InstanceTypeNodeList(i).InnerText = "0" Then 160 ' Console.WriteLine(" Instance type: 0-Single appointment") 161 'ElseIf InstanceTypeNodeList(i).InnerText = "1" Then 162 ' Console.WriteLine(" Instance type: 1-Master recurring appointment") 163 'ElseIf InstanceTypeNodeList(i).InnerText = "2" Then 164 ' Console.WriteLine(" Instance type: 2-Single instance, recurring appointment") 165 'ElseIf InstanceTypeNodeList(i).InnerText = "3" Then 166 ' Console.WriteLine(" Instance type: 3-Exception to a recurring appointment") 167 'Else 168 ' Console.WriteLine(" Instance type: Unknown") 169 ' Console.WriteLine("") 170 'End If 171 172 Next 173 174 Else 175 Console.WriteLine("No calendar items found ...") 176 177 End If 178 179 ' Clean up. 180 ResponseStream.Close() 181 Response.Close() 182 183 Catch ex As Exception 184 185 ' Catch any exceptions. Any error codes from the 186 ' SEARCH method requests on the server will be caught 187 ' here, also. 188 Console.WriteLine(ex.Message) 189 190 End Try 191 192 SaveTextFile("C:\test.txt", output) 193 emailCalendar() 194 Me.Close()