Deleting Items (WebDAV)
Topic Last Modified: 2006-06-12
To delete items using the World Wide Web Distributed Authoring and Versioning (WebDAV) protocol
- Create a WebDAV DELETE Method request. The request Uniform Resource Identifier (URI) for the command is the URI to the item that you want to delete.
- Send the request. The body of the request should be empty.
- If successful, the response status will be "200 OK".
See Constructing Exchange Store HTTP URLs and Authentication and Security Using WebDAV for more information.
This topic contains Microsoft® JScript®, Microsoft Visual C++®, Microsoft C#, and Microsoft Visual Basic® .NET code examples.
The following example shows a typical DELETE Method request:
DELETE /pub2/folder1/folder2 HTTP/1.1
Host: hostname
Content-Length: 0
JScript
The following example uses an instance of the Microsoft.XMLHTTP Component Object Model (COM) class to send a DELETE Method request to an Exchange store server.
Example
// Note: It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
function deleteItem( uri ) {
// Initialize the XMLHTTP request object.
var Req = new ActiveXObject("Microsoft.XMLHTTP");
// Open the request object with the DELETE method and
// specify that it will be sent asynchronously.
Req.open("DELETE", uri, false);
// Send the DELETE method request.
Req.send();
// An error occurred on the server.
if(Req.status >= 500)
{
this.document.writeln("Status: " + Req.status);
this.document.writeln("Status text: An error occurred on the server.");
}
else
{
this.document.writeln("Status: " + Req.status);
this.document.writeln("Status text: " + Req.statustext);
}
return Req;
}
C++
The following example uses the DELETE Method to delete Test.eml from TestFolder.
Example
#include <stdio.h>
#include <iostream.h>
// If necessary, 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);
// Variables.
MSXML::IXMLHttpRequest* pXMLHttpReq=NULL;
bstr_t sUrl = "https://server/public/TestFolder/Test.eml";
bstr_t sMethod = "DELETE";
_variant_t vUser = L"Domain\\Username";
_variant_t vPassword = L"!Password";
_variant_t vAsync = (bool)FALSE;
long lStatus = 0;
BSTR bstrResp;
BSTR bstrResponseText;
HRESULT hr;
// Initialize the XMLHTTPRequest object pointer.
hr = ::CoCreateInstance(__uuidof(XMLHTTPRequest),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IXMLHttpRequest),
(LPVOID*)&pXMLHttpReq);
// If you are using MSXML 4.0, use the following to initialize pXMLHttpReq:
// IXMLHTTPRequestPtr pXMLHttpReq= NULL;
// HRESULT hr=pXMLHttpReq.CreateInstance("Msxml2.XMLHTTP.4.0");
// Check the status of pointer creation.
if (S_OK != hr)
{
cout << "XMLHttpRequest pointer creation failed." << endl;
return 1;
}
try
{
// Open the XMLHTTPRequest object with the DELETE method and
// specify that it will be sent asynchronously.
pXMLHttpReq->open(sMethod,
sUrl,
vAsync,
vUser,
vPassword);
// Send the DELETE method request.
pXMLHttpReq->send();
// Get the response status.
pXMLHttpReq->get_status(&lStatus);
// An error occurred on the server.
if(lStatus == 500)
{
cout << "Status: " << lStatus << endl
<< "Status text: An error occurred on the server."
<< 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;
}
// Release the memory.
pXMLHttpReq->Release();
}
catch(_com_error &e)
{
// Display the error information.
cout << "Error code: " << (char*)e.Error() << endl
<< "Error message: " << (char*)e.ErrorMessage()
<<endl;
// Release the memory.
pXMLHttpReq->Release();
return 1;
}
CoUninitialize();
return 0;
}
C#
The following example uses the System.Net.HttpWebRequest object to send a DELETE Method request to an Exchange store.
Example
using System;
using System.Net;
namespace ExchangeSDK.Snippets.CSharp
{
class DeletingItemsWebDAV
{
[STAThread]
static void Main(string[] args)
{
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strSourceURI = "https://server/public/TestFolder1/test.txt";
string strUserName = "UserName";
string strPassword = "!Password";
string strDomain = "Domain";
try
{
// 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(strSourceURI),
"NTLM",
new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
);
// Create the HttpWebRequest object.
Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strSourceURI);
// Add the network credentials to the request.
Request.Credentials = MyCredentialCache;
// Specify the DELETE method.
Request.Method = "DELETE";
// Send the DELETE method request.
Response = (System.Net.HttpWebResponse)Request.GetResponse();
// Close the HttpWebResponse object.
Response.Close();
Console.WriteLine("Item successfully deleted.");
}
catch(Exception ex)
{
// Catch any exceptions. Any error codes from the DELETE
// method request on the server will be caught here, also.
Console.WriteLine(ex.Message);
}
}
}
}
Visual Basic .NET
The following example uses the System.Net.HttpWebRequest object to send a DELETE Method request to an Exchange store.
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 strSourceURI As String
Try
' Initialize variables.
strUserName = "UserName"
strPassword = "!Password"
strDomain = "Domain"
strSourceURI = "https://server/TestStore/TestStoreFolder/test.txt"
' 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(strSourceURI), _
"NTLM", _
New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _
)
' Create the HttpWebRequest object.
Request = CType(System.Net.WebRequest.Create(strSourceURI), _
System.Net.HttpWebRequest)
' Add the network credentials to the request.
Request.Credentials = MyCredentialCache
' Specify the method.
Request.Method = "DELETE"
' Send the DELETE method request and get the
' response from the server.
Response = CType(Request.GetResponse(), System.Net.HttpWebResponse)
Console.WriteLine("Item successfully deleted.")
' Clean up.
Response.Close()
Catch ex As Exception
' Catch any exceptions. Any error codes from the
' DELETE method requests on the server will be caught
' here, also.
Console.WriteLine(ex.Message)
End Try
End Sub
End Module