Share via


Creating Folders

Topic Last Modified: 2006-06-12

The following examples show how to create a folder in the Exchange store. The function in each example performs the following steps:

  1. The function attempts to create a folder at this URL. If an error occurs, the function fails.
  2. If the function is successful, it sets the new folder's contentclass Field to the value "urn:content-classes:folder".
  3. The function returns a reference to the Record object that is bound to the new folder.

Example

VBScript

Example

If WScript.Arguments.Count < 1 Then
 WScript.Echo "Usage: cscript createfolder.wsf URL [content class]"
 WScript.Quit
End If

Dim sUrl
Dim sContentClass

' Note: It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
sUrl = WScript.Arguments(0)
sContentClass = WScript.Arguments(1)

Dim Rec
Wscript.Echo "Creating folder at URL: " & sUrl
Set Rec = CreateFolder(sUrl, sContentClass, Nothing)
Wscript.Echo "Succeeded."

Function CreateFolder( sUrl, sContentClass, Conn )

 Dim Rec
 Set Rec    = CreateObject("ADODB.Record")

 ' Did caller pass a Connection object reference?
 If Not ( VarType(Conn) = vbObject AND TypeName(Conn) = "Connection" ) Then
   Set Conn = CreateObject("ADODB.Connection")
   Conn.Provider = "ExOLEDB.DataSource"
   Conn.Open sUrl
 End If

 If sContentClass = "" Then
  sContentClass = "urn:content-classes:folder" ' The Default is urn:content-classes:folder.
 End If

 ' Try to create the folder

 Rec.Open sUrl, Conn, adModeReadWrite, adCreateCollection
 Rec.Fields("DAV:contentclass") = sContentClass
 Rec.Fields.Update

 Set CreateFolder = Rec

End Function

The following example creates a new folder by using the native OLE DB interfaces directly in Microsoft® Visual C++®.

Example

#include <oledb.h>
#include <msdasc.h>
#include <comdef.h>

#pragma comment(lib,"oledb.lib")
#pragma comment(lib,"msdasc.lib")

#define BUF_SIZE   4096

// Note: It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
HRESULT CreateFolder( BSTR url, IRow** ppRow )
{

   if(url == NULL)
      return E_INVALIDARG;

   HRESULT            hr            = NULL;
   DWORD            dwBindStatus   = 0;
   IBindResource*      pBRes         = NULL;
   ICreateRow*         pCrRow         = NULL;
   CLSID            clsid_ExOLEDBProviderBinder;

   if(FAILED(hr = CLSIDFromProgID(L"ExOLEDB.Binder", &clsid_ExOLEDBProviderBinder)))
      return hr;

   hr = CoCreateInstance(
      clsid_ExOLEDBProviderBinder,
      NULL,
      CLSCTX_INPROC_SERVER,
      IID_IBindResource,
      (void**)&pBRes);

   if(FAILED(hr))
      return hr;

   hr = pBRes->Bind(
      NULL,
      url,
      DBBINDURLFLAG_READWRITE,
      DBGUID_SESSION,
      IID_ICreateRow,
      NULL,
      NULL,
      &dwBindStatus,
      (IUnknown**) &pCrRow);

   if(FAILED(hr)){
      pBRes->Release();
      return hr;
   }

   pBRes->Release();

   dwBindStatus         =   0;
   IRow*      pRow      =   NULL;
   wchar_t*   newUrl      =   NULL;
   hr = pCrRow->CreateRow(
      NULL,
      url,
      (DBBINDURLFLAG_READWRITE | DBBINDURLFLAG_COLLECTION ),
      DBGUID_ROW,
      IID_IRow,
      NULL,
      NULL,
      &dwBindStatus,
      &newUrl,
      (IUnknown**)&pRow
   );
   if(FAILED(hr)){
      pCrRow->Release();
      return hr;
   }

   *ppRow = pRow;
   pCrRow->Release();

   return hr;
}