Moving Items (WebDAV)

Moving Items (WebDAV)

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.To move items using the World Wide Web Distributed Authoring and Versioning (WebDAV) protocol

  1. Create a WebDAV MOVE Method request. The request Uniform Resource Identifier (URI) for the command is the URI to the item that you want to move.
  2. Set the HTTP Destination Header to the URI for the moved item.
  3. Send the request. The body of the request should be empty.
  4. If successful, the response status will be "201 Created".

The MOVE Method may also be used to rename an item by setting the Destination Header to the collection that the item is located in.

See Constructing Exchange Store HTTP URLs and Authentication and Security Using WebDAV for more information.

This topic contains Microsoft® JScript®, Microsoft Visual Basic® Scripting Edition (VBScript), Microsoft Visual C++®, Microsoft C#, and Visual Basic .NET code examples.

The following example shows a typical MOVE Method request:

MOVE /pub2/folder1/folder2 HTTP/1.1
Destination: /pub2/folder1/folder3
Host: hostname

JScript

The following example uses an instance of the Microsoft.XMLHTTP Component Object Model (COM) class to send a MOVE Method request to an Exchange store server.

// Note: It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
function copyItem( urisource, urito ) {

 // Initialize the XMLHTTP request object.
 var Req = new ActiveXObject("Microsoft.XMLHTTP");

 // Open the request object with the MOVE method and
 // specify that it will be sent asynchronously.
 Req.open("MOVE", urisource, false);

 // Set the Destination header to the destination URL.
 Req.setRequestHeader("Destination",urito);

 // Send the MOVE 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;
}

VBScript

The following example uses the MOVE Method to move test.eml from TestFolder1 to TestFolder2.

Option Explicit

' Variables.
dim strSrcURL
dim strDestURL
dim strUserName
dim strPassword
Dim req

strSrcURL = "https://server/public/TestFolder1/test.eml"
strDestURL = "https://server/public/TestFolder2/test.eml"
strUserName = "Domain\Username"
strPassword = "!Password"

' Initialize the XMLHTTP request object.
Set req = CreateObject("Microsoft.xmlhttp")

' Open the request object with the MOVE method and
' specify that it will be sent asynchronously.
req.open "MOVE", strSrcURL, False, strUserName, strPassword

' Set the Destination header to the destination URL.
req.setRequestHeader "Destination", strDestURL

'Send the MOVE method request.
req.send

' An error occurred on the server.
If req.status >= 500 Then
   wscript.echo "Status: " & req.status
   wscript.echo "Status text: An error occurred on the server."

Else
   wscript.echo "Status: " & req.status
   wscript.echo "Status text: " & req.statustext

End If

C++

The following example uses the MOVE Method to move Test.eml from TestFolder1 to TestFolder2.

#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 sSourceUrl = "https://server/public/TestFolder1/Test.eml";
   bstr_t sDestUrl = "https://server/public/TestFolder2/Test.eml";
   bstr_t sMethod = "MOVE";
   _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 MOVE method and
      // specify that it will be sent asynchronously.
      pXMLHttpReq->open(sMethod,
                        sSourceUrl,
                        vAsync,
                        vUser,
                        vPassword);

      // Set the Destination header.
      pXMLHttpReq->setRequestHeader((bstr_t)"Destination", sDestUrl);

      // Send the MOVE 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 MOVE Method request to an Exchange store.

using System;
using System.Net;

namespace ExchangeSDK.Snippets.CSharp
{
   class MovingItemsWebDAV
   {
      [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 strDestURI = "https://server/public/TestFolder2/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 method.
            Request.Method = "MOVE";

            // Specify the destination URI.
            Request.Headers.Add("Destination", strDestURI);

            // Specify that, if a resource already exists at the
            // destination URI, it will not be overwritten.  The
            // server would return a 412 Precondition Failed status code.
            Request.Headers.Add("Overwrite", "F");

            // Send the MOVE method request.
            Response = Request.GetResponse();

            Console.WriteLine("Item successfully moved.");

            // Close the HttpWebResponse object.
            Response.Close();
         }
         catch(Exception ex)
         {
            // Catch any exceptions. Any error codes from the MOVE
            // 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 MOVE Method request to an Exchange store.

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 strDestURI As String

   Try
      ' Initialize variables.
      strUserName = "UserName"
      strPassword = "!Password"
      strDomain = "Domain"
      strSrcURI = "https://server/TestStore/TestStoreFolder/test.txt"
      strDestURI = "https://server/TestStore/TestStoreFolder2/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(strSrcURI), _
          "NTLM", _
          New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _
          )

      ' Create the HttpWebRequest object.
      Request = CType(System.Net.WebRequest.Create(strSrcURI), _
                      System.Net.HttpWebRequest)

      ' Add the network credentials to the request.
      Request.Credentials = MyCredentialCache

      ' Specify the method.
      Request.Method = "MOVE"

      ' Set the Destination URI.
      Request.Headers.Add("Destination", strDestURI)

      ' Specify that, if a resource already exists at the
      ' destination URI, it will not be overwritten.  The
      ' server would return a 412 Precondition Failed status code.
      Request.Headers.Add("Overwrite", "F")

      ' Send the MOVE method request and get the
      ' response from the server.
      Response = CType(Request.GetResponse(), System.Net.HttpWebResponse)

      Console.WriteLine("Item successfully moved.")

      ' Clean up.
      Response.Close()

   Catch ex As Exception

      ' Catch any exceptions. Any error codes from the
      ' MOVE method requests on the server will be caught
      ' here, also.
      Console.WriteLine(ex.Message)

   End Try

End Sub

End Module

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

This topic last updated: March 2004

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.