Copying Items (WebDAV)
Copying 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 copy items using the World Wide Web Distributed Authoring and Versioning (WebDAV)
- Create a WebDAV COPY Method request. The request Uniform Resource Identifier (URI) for the command is the URI to the item that you want to copy.
- Set the HTTP Destination Header to the URI for the copied item.
- Send the request. The body of the request should be empty.
- If successful, the response status will be "201 Created".
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 COPY Method request:
COPY /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 COPY Method request to an Exchange store.
// 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 COPY method and // specify that it will be sent asynchronously. Req.open("COPY", urisource, false); // Set the Destination header to the destination URL. Req.setRequestHeader("Destination",urito); // Send the COPY 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 COPY Method to copy 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 COPY method and ' specify that it will be sent asynchronously. req.open "COPY", strSrcURL, False, strUserName, strPassword ' Set the Destination header to the destination URL. req.setRequestHeader "Destination", strDestURL 'Send the COPY 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 COPY Method to copy test.eml from TestFolder1 to TestFolder2.
#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); // 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 = "COPY"; _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, then 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 COPY 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 COPY 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 COPY Method request to an Exchange store.
using System; using System.Net; namespace ExchangeSDK.Snippets.CSharp { class CopyingItemsWebDAV { [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 COPY method. Request.Method = "COPY"; // 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 COPY method request. Response = (System.Net.HttpWebResponse)Request.GetResponse(); // Close the HttpWebResponse object. Response.Close(); Console.WriteLine("Item successfully copied."); } catch(Exception ex) { // Catch any exceptions. Any error codes from the COPY // 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 COPY 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/public/TestFolder1/test.txt" strDestURI = "https://server/public/TestFolder2/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 = "COPY" ' 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 COPY method request and get the ' response from the server. Response = CType(Request.GetResponse(), System.Net.HttpWebResponse) Console.WriteLine("Item successfully copied.") ' Clean up. Response.Close() Catch ex As Exception ' Catch any exceptions. Any error codes from the ' COPY method requests on the server will be caught ' here, also. Console.WriteLine(ex.Message) End Try End Sub End Module
Related Topics
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.