Partager via


Accessing Folders and Items

Topic Last Modified: 2006-06-12

The following task shows how to get an existing item in the Exchange store. The example function performs the following steps:

  1. Attempts to bind to the item at this URL. If an error occurs, the function fails.
  2. Returns a reference to the Record object bound to the new item.

Example

VBScript

Example

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

Dim Rec
Dim InfoNT
Dim sUrl
Dim sRelPath
Set InfoNT = CreateObject("WinNTSystemInfo")

sRelPath = "/public/test_folder/item1.txt"
sUrl = "http://" & InfoNT.Computername & sRelPath

Set Rec = getItem_rw(sUrl, Nothing)
WScript.echo "Opened " & Rec.Fields("DAV:displayname") & " at URL: " & Rec.Fields("DAV:href")
' ...

Dim Conn
Set Conn = CreateObject("ADODB.Connection")
Conn.Provider = "ExOLEDB.DataSource"
Conn.Open sUrl
Set Rec = getItem_ro(sUrl, Conn)
WScript.echo "Opened " & Rec.Fields("DAV:displayname") & " at URL: " & Rec.Fields("DAV:href")

' Close connection.
Conn.Close

''''''''''''''''''''''''''''''''''''''''''''''''''''
' getItem_rw
'   sUrl - The URL to the item (http).
'   Conn   - An open Connection object, or Nothing.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''

Function getItem_rw( sUrl, 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

 ' Try to bind to the item.
 Rec.Open sUrl, Conn, adModeReadWrite

 Set getItem_rw = Rec

End Function


''''''''''''''''''''''''''''''''''''''''''''''''''''
' getItem_ro
'   sUrl - The URL to the item (http).
'   Conn   - An open Connection object, or Nothing.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''

Function getItem_ro( sUrl, 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

 ' Try to bind to the item.
 Rec.Open sUrl, Conn

 Set getItem_ro = Rec

End Function
</script>
</job>

The following example demonstrates how to bind to an existing item using the native OLE DB interfaces directly when using Microsoft® Visual C++®. If the item is successfully bound, the OLE DB IRow interface address is returned.

C++

Example

#include <oledb.h>
#include <msdasc.h>
#pragma comment(lib,"oledb.lib")
#pragma comment(lib,"msdasc.lib")

#define BUF_SIZE   4096

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

   if(url == NULL)
      return E_INVALIDARG;

   HRESULT         hr            =  S_OK;
   IBindResource*   pBRes         =  NULL;
   IUnknown*      pUnk         =  NULL;
   DBBINDURLSTATUS dwBindStatus   = 0;
   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_ROW,
      IID_IRow,
      NULL,
      NULL,
      &dwBindStatus,
      (IUnknown**) &pUnk);

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

   hr = pUnk->QueryInterface(ppRow);
   pBRes->Release();
   return hr;

}