Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q314181

↑ Back to the top


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

↑ Back to the top


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

↑ Back to the top


References

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

↑ Back to the top


Keywords: KB314181, kbhowto, kbxml

↑ Back to the top

Article Info
Article ID : 314181
Revision : 4
Created on : 4/1/2004
Published on : 4/1/2004
Exists online : False
Views : 448