Microsoft KB Archive/314191

= How to work with items on an Exchange Server computer by using HttpWebRequest in Visual Basic .NET =

Article ID: 314191

Article Last Modified on 6/10/2005

-

APPLIES TO


 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft XML Core Services 4.0
 * Microsoft XML Core Services 4.0

-



This article was previously published under Q314191




 * INTRODUCTION
 * MORE INFORMATION
 * Create an Appointment item
 * Search for items
 * Copy and move objects



INTRODUCTION
This article contains code samples that perform the following tasks on a computer that is running Microsoft Exchange 2000 Server:
 * Create an Appointment item
 * Search for items
 * Copy and move objects

The code samples use the HttpWebRequest class and the HttpWebResponse class from the System.Net namespace.

back to the top



MORE INFORMATION
To use the HttpWebRequest class and the HttpWebResponse class to work with items in Exchange 2000 Server:
 * 1) Start Microsoft Visual Studio .NET.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Under Project Types, click Visual Basic Projects.
 * 4) Under Templates, click Console Application, and then click OK.

By default, Module1.vb is created.
 * 1) In the code window, replace the existing code with one of the code samples in this article.
 * 2) Search for &quot;TODO&quot; in the code, and then modify the code for your environment.
 * 3) Press F5 to build and to run the program.

back to the top

Create an Appointment item
To create an Appointment item, set the HttpWebRequest.Method property to &quot;PROPPATCH,&quot; send the request to the Exchange Server computer, and then use the HttpWebResponse class to receive the response. Imports System.Net Imports System.IO

Module Module1 Sub Main ' TODO: Replace the following URL with the URL to the new Appointment item. Dim sUri As String = &quot;http://ExchServer/Exchange/Administrator/Calendar/Test.eml&quot;

Dim myUri As System.Uri = New System.Uri(sUri) Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)

Dim strXMLNSInfo As String = &quot;xmlns:g=&quot;&quot;DAV:&quot;&quot; &quot; & _ &quot; xmlns:e=&quot;&quot;http://schemas.microsoft.com/exchange/&quot;&quot;&quot; & _ &quot; xmlns:mapi=&quot;&quot;http://schemas.microsoft.com/mapi/&quot;&quot;&quot; & _ &quot; xmlns:x=&quot;&quot;xml:&quot;&quot; xmlns:cal=&quot;&quot;urn:schemas:calendar:&quot;&quot;&quot; & _ &quot; xmlns:dt=&quot;&quot;urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/&quot;&quot;&quot; & _ &quot; xmlns:mail=&quot;&quot;urn:schemas:httpmail:&quot;&quot;>&quot;

Dim sQuery As String = &quot;&quot; & _ &quot;&quot; & _ &quot;&quot; & _ &quot;urn:content-classes:appointment&quot; & _ &quot;IPM.Appointment&quot; & _ &quot;Appointment Subject&quot; & _ &quot;Appointment Location&quot; & _ &quot;2002-01-07T22:00:00.000Z</cal:dtstart>&quot; & _ &quot;<cal:dtend dt:dt=&quot;&quot;dateTime.tz&quot;&quot;>2002-01-07T22:30:00.000Z</cal:dtend>&quot; & _ &quot;<cal:instancetype dt:dt=&quot;&quot;int&quot;&quot;>0</cal:instancetype>&quot; & _ &quot;<cal:busystatus>BUSY</cal:busystatus>&quot; & _ &quot;<cal:meetingstatus>TENTATIVE</cal:meetingstatus>&quot; & _ &quot;<cal:alldayevent dt:dt=&quot;&quot;boolean&quot;&quot;>0</cal:alldayevent>&quot; & _ &quot;</g:prop>&quot; & _ &quot;</g:set>&quot; & _ &quot;</g:propertyupdate>&quot;

' Set credentials. ' TODO: Replace the following with appropriate user credentials. Dim myCred As NetworkCredential = New NetworkCredential(&quot;Domain\UserName&quot;, &quot;Password&quot;) Dim MyCredentialCache As CredentialCache = New CredentialCache MyCredentialCache.Add(myUri, &quot;Basic&quot;, myCred) HttpWRequest.Credentials = MyCredentialCache

' Set headers. HttpWRequest.KeepAlive = False HttpWRequest.Headers.Set(&quot;Pragma&quot;, &quot;no-cache&quot;) HttpWRequest.Headers.Set(&quot;Translate&quot;, &quot;f&quot;) HttpWRequest.ContentType = &quot;text/xml&quot; HttpWRequest.ContentLength = sQuery.Length

'Set the request timeout to 5 minutes. HttpWRequest.Timeout = 300000 ' set the request method HttpWRequest.Method = &quot;PROPPATCH&quot;

' Store the data in a byte array. Dim ByteQuery As Byte = System.Text.Encoding.ASCII.GetBytes(sQuery) HttpWRequest.ContentLength = ByteQuery.Length Dim QueryStream As Stream = HttpWRequest.GetRequestStream ' Write the data to be posted to the Request stream. QueryStream.Write(ByteQuery, 0, ByteQuery.Length) QueryStream.Close

' Send the request and get a response. Dim HttpWResponse As HttpWebResponse = HttpWRequest.GetResponse

' Get the status and the headers. Dim iStatCode As Integer = HttpWResponse.StatusCode Dim sStatus As String = iStatCode.ToString Console.WriteLine(&quot;Status: {0} {1}&quot;, sStatus, HttpWResponse.StatusDescription.ToString)

Console.WriteLine(&quot;Request Headers:&quot;) Console.WriteLine(HttpWRequest.Headers.ToString) Console.WriteLine(&quot;Response Headers:&quot;) Console.WriteLine(HttpWResponse.Headers.ToString)

' Get the Response stream. Dim strm As Stream = HttpWResponse.GetResponseStream

' Read the Response stream. Dim sr As StreamReader = New StreamReader(strm) Dim sText As String = sr.ReadToEnd Console.WriteLine(&quot;Response: {0}&quot;, sText)

' Close the stream. strm.Close

' Clean up. HttpWRequest = Nothing HttpWResponse = Nothing MyCredentialCache = Nothing myCred = Nothing strm = Nothing sr = Nothing End Sub End Module back to the top

Search for items
To search for items, set the HttpWebRequest.Method property to &quot;SEARCH,&quot; send the query string to the Exchange server in a request, and then use the HttpWebResponse class to receive the response. The query string looks similar to Transact-SQL. Imports System.Net Imports System.IO

Module Module1 Sub Main ' TODO: Replace the following URL with the URL of an object on the Exchange Server computer. Dim sUri As String = &quot;http://ExchServer/Exchange/Administrator/Inbox/&quot;

Dim myUri As System.Uri = New System.Uri(sUri) Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)

' TODO: Search for items whose subject is Test. Dim sQuery As String sQuery = &quot;<?xml version='1.0'?>&quot; & _ &quot;<g:searchrequest xmlns:g='DAV:' >&quot; & _ &quot;<g:sql>SELECT &quot;&quot;DAV:displayname&quot;&quot;, &quot; & _ &quot;&quot;&quot;DAV:href&quot;&quot; &quot; & _ &quot;FROM SCOPE('SHALLOW TRAVERSAL OF &quot;&quot;&quot; & sUri & &quot;&quot;&quot;')&quot; & _ &quot;WHERE &quot;&quot;urn:schemas:mailheader:subject&quot;&quot; = 'Test'&quot; & _ &quot;</g:sql>&quot; & _ &quot;</g:searchrequest>&quot;

' Set credentials. ' TODO: Replace the following with appropriate user credentials. Dim myCred As NetworkCredential = New NetworkCredential(&quot;Domain\UserName&quot;, &quot;Password&quot;) Dim MyCredentialCache As CredentialCache = New CredentialCache MyCredentialCache.Add(myUri, &quot;Basic&quot;, myCred) HttpWRequest.Credentials = MyCredentialCache

' Set some headers. HttpWRequest.KeepAlive = False HttpWRequest.Headers.Set(&quot;Pragma&quot;, &quot;no-cache&quot;)

HttpWRequest.Headers.Set(&quot;Translate&quot;, &quot;f&quot;) HttpWRequest.Headers.Set(&quot;Depth&quot;, &quot;0&quot;) HttpWRequest.ContentType = &quot;text/xml&quot; HttpWRequest.ContentLength = sQuery.Length

'Set the request timeout to 5 minutes. HttpWRequest.Timeout = 300000 ' set the request method HttpWRequest.Method = &quot;SEARCH&quot;

' Store the data in a byte array. Dim ByteQuery As Byte = System.Text.Encoding.ASCII.GetBytes(sQuery) HttpWRequest.ContentLength = ByteQuery.Length Dim QueryStream As Stream = HttpWRequest.GetRequestStream ' write the data to be posted to the Request Stream QueryStream.Write(ByteQuery, 0, ByteQuery.Length) QueryStream.Close

' Send the request and get a response. Dim HttpWResponse As HttpWebResponse = HttpWRequest.GetResponse

' Get the status and the headers. Dim iStatCode As Integer = HttpWResponse.StatusCode Dim sStatus As String = iStatCode.ToString Console.WriteLine(&quot;Status: {0} {1}&quot;, sStatus, HttpWResponse.StatusDescription.ToString)

Console.WriteLine(&quot;Request Headers:&quot;) Console.WriteLine(HttpWRequest.Headers.ToString) Console.WriteLine(&quot;Response Headers:&quot;) Console.WriteLine(HttpWResponse.Headers.ToString)

' Get the Response stream. Dim strm As Stream = HttpWResponse.GetResponseStream

' Read the Response stream. Dim sr As StreamReader = New StreamReader(strm) Dim sText As String = sr.ReadToEnd Console.WriteLine(&quot;Response: {0}&quot;, sText)

' Close the stream. strm.Close

' Clean up. HttpWRequest = Nothing HttpWResponse = Nothing MyCredentialCache = Nothing myCred = Nothing QueryStream = Nothing strm = Nothing sr = Nothing End Sub End Module back to the top

Copy and move objects
To copy or to move objects, create an HttpWebRequest object that is based on the source uniform resource identifier (URI), set the HttpWebRequest.Method property to &quot;COPY&quot; or to &quot;MOVE,&quot; put the destination URI in the header of the request, and then send the request to the Exchange Server computer. Use the HttpWebResponse class to receive the response. Imports System.Net Imports System.IO

Module Module1

Sub Main ' TODO: Replace the following with True for MOVE, or False for COPY. Dim bMove As Boolean = True

' TODO: Replace the following with the source URL. Dim sSourceURL As String = &quot;http://ExchServer/Public/Folder1/Test.EML&quot;

' TODO: Replace the following with the destination URL. Dim sDestinationURL As String = &quot;http://ExchServer/Public/Folder2/Test.EML&quot;

Dim myUri As System.Uri = New System.Uri(sSourceURL) Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)

' Set the credentials. ' TODO: Replace the following with appropriate user credentials. Dim myCred As NetworkCredential = New NetworkCredential(&quot;Domain\UserName&quot;, &quot;Password&quot;) Dim MyCredentialCache As CredentialCache = New CredentialCache MyCredentialCache.Add(myUri, &quot;Basic&quot;, myCred) HttpWRequest.Credentials = MyCredentialCache

' Set headers. HttpWRequest.KeepAlive = False HttpWRequest.Headers.Set(&quot;Pragma&quot;, &quot;no-cache&quot;) HttpWRequest.Headers.Set(&quot;Destination&quot;, sDestinationURL)

'Set the request timeout to 5 minutes. HttpWRequest.Timeout = 300000 ' Set the Request method. If bMove Then HttpWRequest.Method = &quot;MOVE&quot; Else HttpWRequest.Method = &quot;COPY&quot; End If

' Send the request and get the response. Dim HttpWResponse As HttpWebResponse = HttpWRequest.GetResponse

' Get the status and the headers. Dim iStatCode As Integer = HttpWResponse.StatusCode Dim sStatus As String = iStatCode.ToString Console.WriteLine(&quot;Status: {0} {1}&quot;, sStatus, HttpWResponse.StatusDescription.ToString)

Console.WriteLine(&quot;Request Headers:&quot;) Console.WriteLine(HttpWRequest.Headers.ToString) Console.WriteLine(&quot;Response Headers:&quot;) Console.WriteLine(HttpWResponse.Headers.ToString)

' Get the Response stream. Dim strm As Stream = HttpWResponse.GetResponseStream

' Read the Response stream. Dim sr As StreamReader = New StreamReader(strm) Dim sText As String = sr.ReadToEnd Console.WriteLine(&quot;Response: {0}&quot;, sText)

' Close the stream. strm.Close

' Clean up. HttpWRequest = Nothing HttpWResponse = Nothing MyCredentialCache = Nothing myCred = Nothing strm = Nothing sr = Nothing End Sub End Module back to the top

Additional query words: meeting request

Keywords: kbhowto kbhowtomaster KB314191

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.