Share via


Creating Items

Topic Last Modified: 2006-06-12

The following task shows how to create a non-collection item in the Exchange store. The example function performs the following steps:

  1. Attempts to create the item at this URL. If an error occurs, the function fails.
  2. If successful, sets the new item's DAV:contentclass property to the value " urn:content-classes:item".
  3. Returns a reference to the Record object bound to the new item.

Example

VBScript

Example

<job id="createitem">
<reference object="adodb.record"/>
<script language="vbscript">

If WScript.Arguments.Count < 1 Then
 WScript.Echo "Usage: cscript createitem.wsf URL [content class] [content type [, file contents name]]"
 Wscript.Echo "For example:"
 WScript.Echo " cscript createitem.wsf ""https://server/path/item.txt"" """" ""text/plain; charset=iso-8859-1"" <filename>"
 WScript.Quit
End If

Dim sUrl
Dim sContentClass
Dim sContentType
Dim sFileName

' 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)
sContentType  = WScript.Arguments(2)
sFileName = Wscript.Arguments(3)

Dim Rec
Wscript.Echo "Creating item at URL: " & sUrl
Set Rec = CreateItem(sUrl, sContentClass, sContentType, sFileName, Nothing)
Wscript.Echo "Succeeded."
Wscript.Echo Rec.Fields("DAV:href")
WScript.Echo Rec.Fields("DAV:contentclass")
Wscript.Echo Rec.Fields("urn:schemas:mailheader:content-type")

Function CreateItem( sUrl, sContentClass, sContentType, sFileName, 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:item" ' The default content class is urn:content-classes:item.
 End If

 If sContentType = "" Then
  sContentClass = "application/octet-stream"
 End If

 ' Try to create the item

 Rec.Open sUrl, Conn, adModeReadWrite, adCreateNonCollection
 Rec.Fields("DAV:contentclass") = sContentClass
 Rec.Fields("urn:schemas:mailheader:content-type") = sContentType
 Rec.Fields.Update

 If Not sFileName = "" Then
  Set Stm = Rec.Fields(adDefaultStream).Value
  sContentType = LCase(sContentType)
  If InStr(sContentType, "text") > -1 Then
   Stm.Type = adTypeText
   pos = InStr(sContentType, "charset=") + 8
   wscript.echo pos
   If pos > 8 Then
     sCharset = Mid(sContentType, pos)
     wscript.echo "charset: " & sCharset
     Stm.Charset = sCharset
   Else
     Stm.Charset = "US-ASCII"
   End If
  Else
   Stm.Type = adTypeBinary
  End If

  Stm.LoadFromFile sFileName
  Stm.Flush
 End If

' Clean up.
Conn.Close
Stm.Close

 Set CreateItem = Rec

End Function
</script>
</job>

The following example illustrates how to create a new non-collection item and get an existing item 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")

HRESULT CreateItem( BSTR url, IRow** ppRow )
{


   HRESULT            hr               =   S_OK;
   DWORD            dwBindStatus      =   0;
   IBindResource*      pBRes            =   NULL;
   ICreateRow*         pCrRow            =   NULL;
   ITransactionLocal*   pTrans            =   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();

   hr=pCrRow->QueryInterface(IID_ITransactionLocal,(void**)&pTrans);
   if(FAILED(hr))
      pTrans = NULL;
   else
   {
      DWORD dwLevel = 0;
      hr = pTrans->StartTransaction(ISOLATIONLEVEL_UNSPECIFIED,0,NULL,&dwLevel);
   }

   IUnknown*  pUnk   = NULL;
   wchar_t* newUrl;
   hr = pCrRow->CreateRow(
      NULL,
      url,
      DBBINDURLFLAG_READWRITE,
      DBGUID_ROW,
      IID_IRow,
      NULL,
      NULL,
      &dwBindStatus,
      &newUrl,
      &pUnk
   );

   if(FAILED(hr)){
      if(pTrans != NULL) {
         pTrans->Abort(NULL,FALSE,FALSE);
         pTrans->Release();
      }
      pCrRow->Release();
      return hr;
   }

   hr = pUnk->QueryInterface(ppRow);

   hr = pTrans->Commit(FALSE, 0, 0 );
   pTrans->Release();
   pCrRow->Release();
   pUnk->Release();

   return hr;
}