Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Use MKCOL in WebDAV to Create a New Folder


View products that this article applies to.

Summary

This article demonstrates the use of the Web Distributed Authoring and Versioning (WebDAV) MKCOL command to create a new folder.

↑ Back to the top


More information

The following Visual C++ code sample uses the HTTPRequest object to send a MKCOL request to the Exchange server to create a new folder named "Testfolder" of type IPF.Appointment under Public Folders.

This sample requires Msxml.dll version 2.0 or later. To run this sample, follow these steps:
  1. In Visual C++, create a new Win32 console application and name it "Mysample".
  2. Replace the code in the Mysample.cpp file with the following code:
    include<stdio.h>
    
    //TODO: Change the path here if your Msxml.dll file is in a different location.
    
    #import "c:\winnt\system32\msxml.dll"
    
    //To  use MSXML 6.0 import the dll msxml4.dll instead of msxml.dll as follows 
    // #import "c:\winnt\system32\msxml6.dll"
    // using namespace MSXML2;
    
    using namespace MSXML;
    
    int main(int argc, char* argv[])
    {
       CoInitialize(NULL);
       try
       {
       //TODO: Change the line below to reflect your server.
       bstr_t yourServerName = "myserver1";
    
       bstr_t sUrl = "http://" + yourServerName + "/public/Testfolder";
       bstr_t sMethod = "MKCOL";
             
       //TODO: Change the 2 lines below to reflect your user name and password.
       _variant_t vUser = L"myserver1\\User1";
       _variant_t vPassword = L"password";
             
       MSXML::IXMLHttpRequest* pXMLHttpReq=NULL;     
       HRESULT hr = ::CoCreateInstance(__uuidof(XMLHTTPRequest), 
                   NULL, 
                   CLSCTX_INPROC_SERVER, 
                   __uuidof(IXMLHttpRequest), 
                   (LPVOID*)&pXMLHttpReq);
    
       // To use MSXML 6.0 use the following Decleration and creation of pXMLHttpReq
       // IXMLHTTPRequestPtr pXMLHttpReq= NULL;
       // HRESULT hr=pXMLHttpReq.CreateInstance("Msxml2.XMLHTTP.6.0");
    
       if (S_OK != hr)
       {
           printf("XML Http Request pointer creation failed\n");
           return 0;
       }
    
       // Call open function.
       _variant_t vAsync = (bool)FALSE;
       pXMLHttpReq->open(sMethod, 
             sUrl, 
             vAsync, 
             vUser, 
             vPassword);
    
       pXMLHttpReq->setRequestHeader((bstr_t)"Content-Type",
          (bstr_t)"text/xml");
    
       bstr_t sReq;
       sReq =  "<?xml version='1.0'?>";
       sReq = sReq + 
          "<a:propertyupdate xmlns:a=" +
          "'DAV:' xmlns:ex='http://schemas.microsoft.com/exchange/'>";
       sReq = sReq + "<a:set><a:prop>";
       sReq = sReq + "<ex:outlookfolderclass>" +
          "IPF.Appointment" + //TODO: Change to the desired folder class
          "</ex:outlookfolderclass>";
       sReq = sReq + "</a:prop></a:set></a:propertyupdate>";
     
    
       // Send the request to set the search criteria.
       pXMLHttpReq->send(sReq);
    
       // OK, get response.     
       long lStatus;
       pXMLHttpReq->get_status(&lStatus);
    
       printf("\n~~~~~~~~\n%d\n", lStatus);
       BSTR bstrResp;
       pXMLHttpReq->get_statusText(&bstrResp);
       printf("\n~~~~~~~~\n%s\n", (char*)(bstr_t)bstrResp);
    
       _bstr_t bstrAllHeaders;
       bstrAllHeaders = pXMLHttpReq->getAllResponseHeaders();
       printf("\n~~~~~~~~\n%s\n", (char*)bstrAllHeaders);
    
       BSTR bstrResponseText;
       pXMLHttpReq->get_responseText(&bstrResponseText);
       printf("\n~~~~~~~~\n%s\n", (char*)(bstr_t)bstrResponseText);   
       }
       catch(_com_error &e)
       {
          printf("Error\a\a\n\tCode = %08lx\n"
             "\tCode meaning = %s\tSource = %s\n\tDescription = %s\n",
          e.Error(), 
          e.ErrorMessage(), 
          (char*)e.Source(), 
          (char*)e.Description() );
       }
    
       CoUninitialize(); 
       
       return 0;
    } 
    					
  3. Make the changes marked by "TODO:" in the code.
  4. Compile and then run the code.

    At the end, you will see the Status as "200 OK". The StatusText will display the status as "HTTP/1.1 201 Created" if the folder was created. You can then go to the Exchange System Manager and you will see the newly created Testfolder listed and it will be of type IPF.Appointment.

↑ Back to the top


Keywords: kbmsg, kbarchive, kbhowto, kbnosurvey, KB289871

↑ Back to the top

Article Info
Article ID : 289871
Revision : 3
Created on : 2/28/2014
Published on : 2/28/2014
Exists online : False
Views : 584