Getting Item Property Values (WebDAV)
Topic Last Modified: 2006-06-12
This example shows how you can construct the XML body of a WebDAV PROPFIND Method manually. The request is for the displayname Field for a folder. After the request has been constructed, the code passes the XML string to an XMLHTTP Component Object Model (COM) object and sends the PROPFIND Method request to the server.
This topic contains Microsoft® Visual Basic® Scripting Edition (VBScript), Microsoft Visual C++®, Microsoft C#, and Visual Basic .NET code examples. See Constructing Exchange Store HTTP URLs and Authentication and Security Using WebDAV for more information.
The following example shows what the request might look like (the code has been formatted for clarity):
PROPFIND /public/docs/ HTTP/1.1
Content-Type: text/xml; charset="UTF-8"
Content-Length: XXX
Depth: 1
...
<?xml version="1.0" encoding="UTF-8"?>
<d:propfind xmlns:d="DAV:">
<d:prop><d:displayname/></d:prop>
</d:propfind>
Example
VBScript
This example is provided mainly for illustration. It is easy to make errors when constructing XML streams manually, so you should use an XML Document Object Model (DOM) implementation to do so.
Example
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>Simple PROPFIND</TITLE>
</HEAD>
<BODY LANGUAGE="VBScript" onLoad="doXML">
<SCRIPT FOR=WINDOW LANGUAGE=VBSCRIPT>
<!--
Sub DoXML
Dim objX, strR, objXD, objDE
Set objX = CreateObject("Microsoft.XMLHTTP")
objX.Open "PROPFIND", "http://myServer/public/docs/", FALSE, "Domain\user", "!Password"
strR = "<?xml version='1.0'?>"
strR = strR & "<d:propfind xmlns:d='DAV:'>"
strR = strR & "<d:prop><d:displayname/></d:prop></d:propfind>"
objX.SetRequestHeader "Content-type:", "text/xml"
objX.SetRequestHeader "Depth", "1"
objX.send(strR)
set docback = objX.responseXML
Dim objNodeList
Set objNodeList = docback.getElementsByTagName("*")
For i = 0 TO (objNodeList.length -1)
Set objNode = objNodeList.nextNode
Document.Write("<b>" & objNode.NamespaceURI & " " & objNode.NodeName & "</b> - ")
Document.Write(objNode.Text & "<hr>")
End Sub
//-->
</SCRIPT>
<P> </P>
</BODY>
</HTML>
C++
The following example uses the PROPFIND Method to get the value of the displayname Field from test.eml.
Example
#include <stdio.h>
#include <iostream.h>
// If neccessary, change the file path if your msxml.dll file is
// in a different location.
#import "c:\windows\system32\msxml.dll"
// To use MSXML 4.0, import the dll msxml4.dll instead of msxml.dll, as follows:
// #import "c:\windows\system32\msxml4.dll"
// using namespace MSXML2;
using namespace MSXML;
int main(int argc, char* argv[])
{
CoInitialize(NULL);
try
{
// Variables.
bstr_t sUrl = "https://Server/public/TestFolder1/test.eml";
bstr_t sMethod = "PROPFIND";
_variant_t vUser = L"Domain\\Username";
_variant_t vPassword = L"!Password";
_variant_t vAsync = (bool)FALSE;
bstr_t sReq;
long lStatus;
BSTR bstrResp;
BSTR bstrResponseText;
HRESULT hr;
// If you are using MSXML 2.0, use the following to initialize pXMLHttpReq:
MSXML::IXMLHttpRequestPtr pXMLHttpReq=NULL;
// If you are using MSXML 4.0, use the following to initialize pXMLHttpReq:
//MSXML2::IXMLHTTPRequestPtr pXMLHttpReq=NULL;
// Initialize the XMLHTTPRequest object pointer.
hr = pXMLHttpReq.CreateInstance(__uuidof(XMLHTTPRequest));
// Check the status of pointer creation.
if (S_OK != hr)
{
cout<< "XML Http Request pointer creation failed. << endl;
return 1;
}
// Open the XMLHTTPRequest object with the PROPFIND method and
// specify that it will be sent asynchronously.
pXMLHttpReq->open(sMethod,
sUrl,
vAsync,
vUser,
vPassword);
// Set the Content-Type header to "text/xml".
pXMLHttpReq->setRequestHeader((bstr_t)"Content-Type",
(bstr_t)"text/xml");
// Build the request body and search for the display name of the
// item.
sReq = "<?xml version='1.0'?>";
sReq = sReq + "<d:propfind xmlns:d='DAV:'><d:prop>" +
"<d:displayname/></d:prop></d:propfind>";
// Send the PROPFIND method request.
pXMLHttpReq->send(sReq);
// Get the method request status.
pXMLHttpReq->get_status(&lStatus);
// An error occurred on the server.
if(lStatus >= 500)
{
pXMLHttpReq->get_statusText(&bstrResp);
cout << "Status: " << lStatus << endl
<< "Status text: An error occurred on the server."
<< endl
<< (char*)(bstr_t)bstrResp
<< endl;
}
// The method request was successful.
else if(lStatus == 207)
{
// Variables.
MSXML::IXMLDOMDocumentPtr pDOMDoc = NULL;
MSXML::IXMLDOMNodeListPtr pDOMNodeList = NULL;
MSXML::IXMLDOMNodePtr pDOMNode = NULL;
BSTR bstrText;
// Create an instance of the DOM Document.
HRESULT hr = pDOMDoc.CreateInstance(__uuidof(DOMDocument));
// Check the status of pointer creation.
if(FAILED(hr))
cout<<"Creation of DOMDocument failed."<<endl;
// Get the method response XML text.
pXMLHttpReq->get_responseText(&bstrText);
// Load the XML document with the response text.
pDOMDoc->loadXML(bstrText);
// Build a list of the DAV:displayname XML nodes (there should
// be only one) corresponding to the folders returned in the
// search request. The DAV: namespace is typically assigned
// the a: prefix in the XML response body.
pDOMNodeList = pDOMDoc->getElementsByTagName((bstr_t)"a:displayname");
// Display the display name of the resource.
pDOMNode = pDOMNodeList->nextNode();
cout << pDOMNode->text << endl;
}
else
{
// Display the response status.
cout << "Status: " << lStatus << endl;
// Display the response status text.
pXMLHttpReq->get_statusText(&bstrResp);
cout << "Status text: " << (char*)(bstr_t)bstrResp << endl;
// Display the response text.
pXMLHttpReq->get_responseText(&bstrResponseText);
cout << "Response text: " << (char*)(bstr_t)bstrResponseText << endl;
}
}
catch(_com_error &e)
{
// Display the error information.
cout << "Error code: " << (char*)e.Error() << endl
<< "Error message: " << (char*)e.ErrorMessage()
<<endl;
return 1;
}
CoUninitialize();
return 0;
}
C#
The following example uses the PROPFIND Method to get the value of the displayname Field from test.eml.
Example
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
namespace ExchangeSDK.Snippets.CSharp
{
class GettingItemPropertyValuesWebDAV
{
[STAThread]
static void Main(string[] args)
{
// Variables.
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strSrcURI = "https://Server/public/TestFolder1/test.eml";
string strUserName = "UserName";
string strPassword = "!Password";
string strDomain = "Domain";
string strBody = "";
byte[] bytes = null;
System.IO.Stream RequestStream = null;
System.IO.Stream ResponseStream = null;
XmlDocument ResponseXmlDoc = null;
XmlNodeList DisplayNameNodes = null;
try
{
// Build the PROPFIND request body.
strBody = "<?xml version=\"1.0\"?>"
+ "<d:propfind xmlns:d='DAV:'><d:prop>"
+ "<d:displayname/></d:prop></d:propfind>";
// Create a new CredentialCache object and fill it with the network
// credentials required to access the server.
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add( new System.Uri(strSrcURI),
"NTLM",
new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
);
// Create the HttpWebRequest object.
Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strSrcURI);
// Add the network credentials to the request.
Request.Credentials = MyCredentialCache;
// Specify the method.
Request.Method = "PROPFIND";
// Encode the body using UTF-8.
bytes = Encoding.UTF8.GetBytes((string)strBody);
// Set the content header length. This must be
// done before writing data to the request stream.
Request.ContentLength = bytes.Length;
// Get a reference to the request stream.
RequestStream = Request.GetRequestStream();
// Write the request body to the request stream.
RequestStream.Write(bytes, 0, bytes.Length);
// Close the Stream object to release the connection
// for further use.
RequestStream.Close();
// Set the content type header.
Request.ContentType = "text/xml";
// Send the PROPFIND method request and get the
// response from the server.
Response = (HttpWebResponse)Request.GetResponse();
// Get the XML response stream.
ResponseStream = Response.GetResponseStream();
// Create the XmlDocument object from the XML response stream.
ResponseXmlDoc = new XmlDocument();
ResponseXmlDoc.Load(ResponseStream);
// Build a list of the DAV:href XML nodes, corresponding to the folders
// in the mailbox. The DAV: namespace is typically assgigned the a:
// prefix in the XML response body.
DisplayNameNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname");
if(DisplayNameNodes.Count > 0)
{
// Display the item's display name.
Console.WriteLine("DAV:displayname property...");
Console.WriteLine(DisplayNameNodes[0].InnerText);
}
else
{
Console.WriteLine("DAV:displayname property not found...");
}
// Clean up.
ResponseStream.Close();
Response.Close();
}
catch(Exception ex)
{
// Catch any exceptions. Any error codes from the PROPFIND
// method request on the server will be caught here, also.
Console.WriteLine(ex.Message);
}
}
}
}
Visual Basic .NET
The following example uses the PROPFIND Method to get the value of the displayname Field from test.eml.
Example
Option Explicit On
Option Strict On
Module Module1
Sub Main()
' Variables.
Dim Request As System.Net.HttpWebRequest
Dim Response As System.Net.HttpWebResponse
Dim MyCredentialCache As System.Net.CredentialCache
Dim strPassword As String
Dim strDomain As String
Dim strUserName As String
Dim strSrcURI As String
Dim strBody As String
Dim bytes() As Byte
Dim RequestStream As System.IO.Stream
Dim ResponseStream As System.IO.Stream
Dim ResponseXmlDoc As System.Xml.XmlDocument
Dim DisplayNameNodes As System.Xml.XmlNodeList
Try
' Initialize variables.
strUserName = "UserName"
strPassword = "!Password"
strDomain = "Domain"
strSrcURI = "https://Server/public/TestFolder1/test.eml"
' Build the PROPFIND request body.
strBody = "<?xml version=""1.0""?>" _
& "<d:propfind xmlns:d='DAV:'><d:prop>" _
& "<d:displayname/></d:prop></d:propfind>"
' Create a new CredentialCache object and fill it with the network
' credentials required to access the server.
MyCredentialCache = New System.Net.CredentialCache
MyCredentialCache.Add(New System.Uri(strSrcURI), _
"NTLM", _
New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _
)
' Create the PUT HttpWebRequest object.
Request = CType(System.Net.WebRequest.Create(strSrcURI), _
System.Net.HttpWebRequest)
' Add the network credentials to the request.
Request.Credentials = MyCredentialCache
' Specify the PROPFIND method.
Request.Method = "PROPFIND"
' Encode the body using UTF-8.
bytes = System.Text.Encoding.UTF8.GetBytes(strBody)
' Set the content header length. This must be
' done before writing data to the request stream.
Request.ContentLength = bytes.Length
' Get a reference to the request stream.
RequestStream = Request.GetRequestStream()
' Write the message body to the request stream.
RequestStream.Write(bytes, 0, bytes.Length)
' Close the Stream object to release the connection
' for further use.
RequestStream.Close()
' Set the Content Type header.
Request.ContentType = "text/xml"
' Set the Depth header.
Request.Headers.Add("Depth", "1")
' Set the Translate header.
Request.Headers.Add("Translate", "F")
' Send the PROPFIND method request and get the
' response from the server.
Response = CType(Request.GetResponse(), System.Net.HttpWebResponse)
' Get the XML response stream.
ResponseStream = Response.GetResponseStream()
' Create the XmlDocument object from the XML response stream.
ResponseXmlDoc = New System.Xml.XmlDocument
ResponseXmlDoc.Load(ResponseStream)
' Build a list of the DAV:href XML nodes, corresponding to the folders
' in the mailbox. The DAV: namespace is typically assgigned the a:
' prefix in the XML response body.
DisplayNameNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname")
If DisplayNameNodes.Count > 0 Then
' Display the item's display name.
Console.WriteLine("DAV:displayname property...")
Console.WriteLine(DisplayNameNodes(0).InnerText)
Else
Console.WriteLine("DAV:displayname property not found...")
End If
' Clean up.
ResponseStream.Close()
Response.Close()
Catch ex As Exception
' Catch any exceptions. Any error codes from the
' PROPFIND method requests on the server will be caught
' here, also.
Console.WriteLine(ex.Message)
End Try
End Sub
End Module