How to work with items in Exchange 2000 by using WebDAV in Visual Basic .NET (314181)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Exchange 2000 Server
  • Microsoft XML 3.0
  • Microsoft XML 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:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. Under Visual Basic Projects types, click Console Application.

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

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 = "http://EXCHANGESERVER/Exchange/UserAlias/Inbox"

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

        ' Set up the query.
        sQuery = "<?xml version='1.0'?>" & _
                 "<g:searchrequest xmlns:g='DAV:'>" & _
                 "<g:sql>SELECT ""DAV:displayname"" " & _
                 "FROM SCOPE('SHALLOW TRAVERSAL OF """ & sUrl & """')"
        ' TODO: Make DAV:isfolder = true if you want to retrieve subfolders,
        ' and make DAV:isfolder = false to retrieve items.
        sQuery = sQuery & " WHERE ""DAV:isfolder"" = false" & _
                 "</g:sql>" & _
                 "</g:searchrequest>"

        ' Set up request headers.
        oXMLHttp.setRequestHeader("Content-Type", "text/xml")
        oXMLHttp.setRequestHeader("Translate", "f")
        oXMLHttp.setRequestHeader("Depth", "0")
        oXMLHttp.setRequestHeader("Content-Length", "" & 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 = "http://EXCHANGESERVER/Exchange/UserAlias/Inbox/Testing.eml"

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

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

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

        ' 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 = "http://EXCHANGESERVER/Exchange/UserAlias/Inbox/Testing.eml"

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

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

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

        ' 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 = "descriptor"

        ' TODO: Replace with your item URL.

        sUrl = "http://EXCHANGESERVER/Exchange/UserAlias/Inbox/Testing.eml"

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

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


        ' Set up request headers.
        oXMLHttp.setRequestHeader("Content-Type", "text/xml")
        oXMLHttp.setRequestHeader("Translate", "f")
        oXMLHttp.setRequestHeader("Depth", "0")
        oXMLHttp.setRequestHeader("Content-Length", "" & 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 = "http://EXCHANGESERVER/Exchange/UserAlias/Inbox/Testing.eml"
        sDestinationUrl = "http://EXCHANGESERVER/Exchange/UserAlias/Inbox/SubFolder/Testing.eml"

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

        ' Set up request headers.
        oXMLHttp.setRequestHeader("Destination", 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 = "http://EXCHANGESERVER/Exchange/UserAlias/Inbox/SubFolder/Testing.eml"

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

        ' 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 = "http://EXCHANGESERVER/Exchange/UserAlias/Inbox/SubFolder"

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

        ' Send the query.
        oXMLHttp.send()

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

        ' Cleanup.
        oXMLHttp = Nothing
    End Sub
End Module

REFERENCES

For additional information about WebDAV methods, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:4/1/2004
Keywords:kbXML kbhowto KB314181 kbAudDeveloper