Microsoft KB Archive/314181

= How to work with items in Exchange 2000 by using WebDAV in Visual Basic .NET =

Article ID: 314181

Article Last Modified on 4/1/2004

-

APPLIES TO


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

-



This article was previously published under Q314181



INTRODUCTION
This article describes how to use World Wide Web Distributed Authoring and Versioning (WebDAV) to work with items in a Microsoft Exchange 2000 store by using Microsoft Visual Basic .NET. This article has samples of code that show the following:
 * How to list folders and list items
 * How to retrieve properties for items
 * How to modify properties for items
 * How to retrieve a security descriptor for an item
 * How to copy items and move items
 * How to delete folders and delete items



MORE INFORMATION
To use the following samples of code, you have to add a reference to the Microsoft XML v3.0 component. To do this, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Under Visual Basic Projects types, click Console Application.

By default, the Module1.vb file is created. To add a reference to Microsoft XML v3.0, follow these steps:  On the Project menu, click Add Reference. On the COM tab, locate Microsoft XML v3.0, and then click Select. In the Add References dialog box, click OK to accept your selections.</li> Click Yes if you receive a message to generate wrappers for the libraries that you selected.</li></ol> </li></ol>

How to list folders and list items
The following sample code lists all subfolders and all items in the Inbox for the user. Imports System.Reflection

Module Module1

Sub Main Dim oXMLHttp As MSXML2.XMLHTTP30 = New MSXML2.XMLHTTP30 Dim sUrl As String Dim sQuery As String

' TODO: Replace with your folder URL.

sUrl = &quot;http://EXCHANGESERVER/Exchange/UserAlias/Inbox&quot;

' Open the folder. oXMLHttp.open(&quot;SEARCH&quot;, sUrl, False, &quot;DOMAIN\UserName&quot;, &quot;Password&quot;)

' Set up the query. 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;FROM SCOPE('SHALLOW TRAVERSAL OF &quot;&quot;&quot; & sUrl & &quot;&quot;&quot;')&quot; ' TODO: Make DAV:isfolder = true if you want to retrieve subfolders, ' and make DAV:isfolder = false to retrieve items. sQuery = sQuery & &quot; WHERE &quot;&quot;DAV:isfolder&quot;&quot; = false&quot; & _ &quot;</g:sql>&quot; & _ &quot;</g:searchrequest>&quot;

' Set up request headers. oXMLHttp.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml&quot;) oXMLHttp.setRequestHeader(&quot;Translate&quot;, &quot;f&quot;) oXMLHttp.setRequestHeader(&quot;Depth&quot;, &quot;0&quot;) oXMLHttp.setRequestHeader(&quot;Content-Length&quot;, &quot;&quot; & sQuery.Length)

' Send the query. oXMLHttp.send(sQuery)

Console.WriteLine(oXMLHttp.status) Console.WriteLine(oXMLHttp.statusText) Console.WriteLine(oXMLHttp.responseText)

' Cleanup. oXMLHttp = Nothing End Sub

End Module

How to retrieve properties for items
The following sample code retrieves the subject, the sender, and the recipient properties for the Testing.eml file in the Inbox for the user. Imports System.Reflection

Module Module1

Sub Main Dim oXMLHttp As MSXML2.XMLHTTP30 = New MSXML2.XMLHTTP30 Dim sUrl As String Dim sQuery As String

' TODO: Replace with your item URL.

sUrl = &quot;http://EXCHANGESERVER/Exchange/UserAlias/Inbox/Testing.eml&quot;

' Open the item. oXMLHttp.open(&quot;PROPFIND&quot;, sUrl, False, &quot;DOMAIN\UserName&quot;, &quot;Password&quot;)

' Set up the query to get subject, from, and to. sQuery = &quot;<?xml version='1.0'?>&quot; & _ &quot;<a:propfind xmlns:a='DAV:' xmlns:m='urn:schemas:mailheader:'>&quot; & _ &quot;<a:prop>&quot; & _ &quot;<m:subject/>&quot; & _ &quot;</a:prop>&quot; & _ &quot;<a:prop>&quot; & _ &quot;<m:from/>&quot; & _ &quot;</a:prop>&quot; & _ &quot;<a:prop>&quot; & _ &quot;<m:to/>&quot; & _ &quot;</a:prop>&quot; & _ &quot;</a:propfind>&quot;

' Set up request headers. oXMLHttp.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml&quot;)

' Send the query. oXMLHttp.send(sQuery)

Console.WriteLine(oXMLHttp.status) Console.WriteLine(oXMLHttp.statusText) Console.WriteLine(oXMLHttp.responseText)

' Cleanup. oXMLHttp = Nothing End Sub

End Module

How to modify properties for items
The following sample code changes the subject of the Testing.eml file in the Inbox for the user: Imports System.Reflection

Module Module1

Sub Main Dim oXMLHttp As MSXML2.XMLHTTP30 = New MSXML2.XMLHTTP30 Dim sUrl As String Dim sQuery As String

' TODO: Replace with your item URL

sUrl = &quot;http://EXCHANGESERVER/Exchange/UserAlias/Inbox/Testing.eml&quot;

' Open the item oXMLHttp.open(&quot;PROPPATCH&quot;, sUrl, False, &quot;DOMAIN\UserName&quot;, &quot;Password&quot;)

' Set up the query to modify the subject sQuery = &quot;<?xml version='1.0'?>&quot; & _ &quot;<a:propertyupdate xmlns:a='DAV:' &quot; & _ &quot;xmlns:m='urn:schemas:mailheader:'>&quot; & _ &quot;<a:set><a:prop>&quot; & _ &quot;<m:subject>&quot; & &quot;ModifiedSubject&quot; & &quot;</m:subject>&quot; & _ &quot;</a:prop></a:set>&quot; & _ &quot;</a:propertyupdate>&quot;

' Set up request headers oXMLHttp.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml&quot;)

' Send the query oXMLHttp.send(sQuery)

Console.WriteLine(oXMLHttp.status) Console.WriteLine(oXMLHttp.statusText) Console.WriteLine(oXMLHttp.responseText)

' Cleanup oXMLHttp = Nothing End Sub

End Module

How to retrieve a security descriptor for an item
The following sample code retrieves the security descriptor for the Testing.eml file in the Inbox for the user: Imports System.Reflection

Module Module1

Sub Main Dim oXMLHttp As MSXML2.XMLHTTP30 = New MSXML2.XMLHTTP30 Dim sUrl As String Dim sQuery As String Dim strSDType As String = &quot;descriptor&quot;

' TODO: Replace with your item URL.

sUrl = &quot;http://EXCHANGESERVER/Exchange/UserAlias/Inbox/Testing.eml&quot;

' Open the item. oXMLHttp.open(&quot;PROPFIND&quot;, sUrl, False, &quot;DOMAIN\UserName&quot;, &quot;Password&quot;)

' Set up the query to get the security descriptor. sQuery = &quot;<?xml version='1.0' encoding='utf-8'?>&quot; & _ &quot;<propfind xmlns=&quot;&quot;DAV:&quot;&quot;>&quot; & _ &quot;<prop xmlns:r=&quot;&quot;http://schemas.microsoft.com/exchange/security/&quot;&quot;>&quot; & _ &quot; &quot; & _ &quot;<r:&quot; & strSDType & &quot;/>&quot; & _ &quot; &quot; & _ &quot; &quot;

' Set up request headers. oXMLHttp.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml&quot;) oXMLHttp.setRequestHeader(&quot;Translate&quot;, &quot;f&quot;) oXMLHttp.setRequestHeader(&quot;Depth&quot;, &quot;0&quot;) oXMLHttp.setRequestHeader(&quot;Content-Length&quot;, &quot;&quot; & sQuery.Length)

' Send the query. oXMLHttp.send(sQuery)

Console.WriteLine(oXMLHttp.status) Console.WriteLine(oXMLHttp.statusText) Console.WriteLine(oXMLHttp.responseText)

' Cleanup. oXMLHttp = Nothing End Sub

End Module

How to copy items and move items
The following sample code moves the Testing.eml file from the Inbox for the user to a subfolder in the Inbox that is named SubFolder: Imports System.Reflection

Module Module1

Sub Main Dim oXMLHttp As MSXML2.XMLHTTP30 = New MSXML2.XMLHTTP30 Dim sSourceUrl As String Dim sDestinationUrl As String

' TODO: Replace with your item URL. sSourceUrl = &quot;http://EXCHANGESERVER/Exchange/UserAlias/Inbox/Testing.eml&quot; sDestinationUrl = &quot;http://EXCHANGESERVER/Exchange/UserAlias/Inbox/SubFolder/Testing.eml&quot;

' Open the source item. ' TODO: Use &quot;MOVE&quot; to move the item, &quot;COPY&quot; to copy it. oXMLHttp.open(&quot;COPY&quot;, sSourceUrl, False, &quot;DOMAIN\UserName&quot;, &quot;Password&quot;) 'oXMLHttp.open(&quot;MOVE&quot;, sSourceUrl, False, &quot;DOMAIN\UserName&quot;, &quot;Password&quot;)

' Set up request headers. oXMLHttp.setRequestHeader(&quot;Destination&quot;, DestinationUrl)

' Send the query. oXMLHttp.send

Console.WriteLine(oXMLHttp.status) Console.WriteLine(oXMLHttp.statusText) Console.WriteLine(oXMLHttp.responseText)

' Cleanup. oXMLHttp = Nothing End Sub

End Module

How to delete folders and delete items
The following sample code deletes the Testing.eml file from a folder that is named SubFolder in the Inbox for the user. The following sample code then deletes the SubFolder folder: Imports System.Reflection

Module Module1

Sub Main Dim oXMLHttp As MSXML2.XMLHTTP30 = New MSXML2.XMLHTTP30 Dim sUrl As String

' TODO: Replace with your item URL. sUrl = &quot;http://EXCHANGESERVER/Exchange/UserAlias/Inbox/SubFolder/Testing.eml&quot;

' Open the source item. oXMLHttp.open(&quot;DELETE&quot;, sUrl, False, &quot;DOMAIN\UserName&quot;, &quot;Password&quot;)

' Send the query. oXMLHttp.send

Console.WriteLine(oXMLHttp.status) Console.WriteLine(oXMLHttp.statusText) Console.WriteLine(oXMLHttp.responseText)

' Now, delete the folder. ' TODO: Replace with your folder URL. sUrl = &quot;http://EXCHANGESERVER/Exchange/UserAlias/Inbox/SubFolder&quot;

' Open the folder. oXMLHttp.open(&quot;DELETE&quot;, sUrl, False, &quot;DOMAIN\UserName&quot;, &quot;Password&quot;)

' Send the query. oXMLHttp.send

Console.WriteLine(oXMLHttp.status) Console.WriteLine(oXMLHttp.statusText) Console.WriteLine(oXMLHttp.responseText)

' Cleanup. oXMLHttp = Nothing End Sub End Module

Additional query words: webdav folder outlook xmlhttp

<div class="references_section">