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.
- 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