Sdílet prostřednictvím


Creating Items (WebDAV)

Topic Last Modified: 2006-06-12

To create non-folder items using the World Wide Web Distributed Authoring and Versioning (WebDAV) protocol

  1. Create a WebDAV PUT Method or PROPPATCH Method request. The request Uniform Resource Identifier (URI) for the command is the URI to the item that you are creating.
  2. When using a PUT Method, set the Translate: HTTP header to the value "f" (false).
  3. When using a PUT Method, send the stream for the item as the request body.
  4. When using a PROPPATCH Method, send the values of the properties in the request body.
  5. Set the Content-Type: header to the appropriate value, for example "text/html; charset=\"iso-8859-1\"" or "text/xml".
  6. Send the request.
  7. If successful, the response status will be "201 Created".

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

The following code shows a typical PUT Method request:

PUT /pub2/folder1/folder2/item1.txt HTTP/1.1
Host: hostname
Translate: f
Content-Type: text/plain
Content-Length: 4

abcd

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

JScript

Example

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

 var Req = new ActiveXObject("Microsoft.XMLHTTP");
 Req.open("PROPPATCH", uri, false);
 if(vXML != "") {
  Req.setRequestHeader("Content-Type","text/xml");
  Req.send(vXML);
 }
 else {
  Req.send();
 }
 return Req;

}

// This example uses a PUT command to put the stream for an item.
// If the item does not exist, it is created.
function putItemStream( uri, text ){
 var Req = new ActiveXObject("Microsoft.XMLHTTP");
 Req.open("PUT", uri, false);
 Req.setRequestHeader("Translate","f");
 if(text != "") {
  Req.setRequestHeader("Content-Type","text/plain");
  Req.send(text);
 }
 else {
  Req.send("");
 }
 return Req;


}

The following example uses the PUT method to create a text file in TestFolder1.

C++

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/TestFolder1/test1.txt";
   bstr_t sMethod = "PUT";
   _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 PUT method and
      // specify that it will be sent asynchronously.
      pXMLHttpReq->open(sMethod,
                        sUrl,
                        vAsync,
                        vUser,
                        vPassword);

      // Set the Content-Type header.
      pXMLHttpReq->setRequestHeader((bstr_t)"Content-Type", (bstr_t)"text/plain");

      // Set Translate header.
      pXMLHttpReq->setRequestHeader((bstr_t)"Translate", (bstr_t)"F");

      bstr_t sReq = "This is a text file.";

      // Send the PUT method request.
      pXMLHttpReq->send(sReq);

      // 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;
}   

To create a folder item, use the WebDAV MKCOL Method.