Microsoft Knowledge Base Email Alertz

(314181) - Describes how to use WebDAV to work with items in an Exchange 2000 store by using Visual Basic .NET. This article has many samples of code that show you how to do this.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 314181 - Last Review: April 1, 2004 - Revision: 1.3

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

This article was previously published under Q314181

On This Page

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:
http://msdn.microsoft.com/en-us/library/aa142917.aspx (http://msdn.microsoft.com/en-us/library/aa142917.aspx)

APPLIES TO
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Exchange 2000 Server Standard Edition
  • Microsoft XML Parser 3.0
  • Microsoft XML Core Services 4.0
Keywords: 
kbxml kbhowto KB314181
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate