Share via


Using Relative URLs

Topic Last Modified: 2006-06-12

When using the Exchange OLE DB (ExOLEDB) provider, all operations on items in a particular public store or mailbox store run in an OLE DB session. After this session has been established with a particular store, you can refer to items in that store by using relative URLs. These operations are called scoped operations, the scope being the folder tree residing in that particular store. With HTTP: URLs, the relative URLs must include the entire path, starting from the root virtual directory. With File: URLs, the path starts with the first folder below the top-level folder. You can also use relative URLs when you construct Structured Query Language (SQL) queries for folders in a store.

With Microsoft® ActiveX® Data Objects (ADO), the OLE DB session object that houses the current session information is wrapped using an instance of an ADO Connection object. After you have established the session (Connection) to a particular store, you can then dispense with generating the fully qualified URL for items in that store when you use ADO Record and Recordset objects.

For example, the following functions demonstrate this functionality in a public store:

  • The getConnection function returns an ADO Connection object that is connected to a particular store. To create the Connection using The File: URL Scheme, you need only specify the name of the top-level public folder within that store. The fully qualified domain name is retrieved using the ADSystemInfo object. This is all the information you need to connect to a public folder tree in a public store. However, for mailboxes, you also need the user's alias.
  • The getRecord_ro function returns a read-only ADO Record object bound to the item specified using a relative URL in a particular store. The caller can pass either an open Connection object or the name of the top-level public folder. In the latter case, the function uses the getConnection function internally to retrieve a Connection to the store identified by the specified top-level public folder.
  • The getRecord_rw function returns a read-write ADO Record object bound to the item specified using a relative URL. The caller can pass either an open Connection object or the name of the top-level public folder. In the latter case, it uses the getConnection function internally to retrieve a Connection to the store identified by the specified top-level public folder.

Example

VBScript

Example

Const adModeReadWrite = 3

Dim Info      'As String
Dim InfoNT
Dim Domain    'As String
Dim Url       'As String
Dim relUrl    'As String
Dim Conn      'As ADODB.Connection

Set InfoNT = CreateObject("WinNTSystemInfo")
Set Info   = CreateObject("ADSystemInfo")
Set Conn = CreateObject("ADODB.Connection")
relUrl = "/public/test.eml"

Url = "http://" & lcase(InfoNT.ComputerName) & "." & Info.DomainDNSName & relUrl
wscript.echo Url
Conn.Provider = "ExOLEDB.DataSource"
Conn.Open Url

Conn.Close

' getRecord_ro
'
'   vSource is either the name of a top-level public folder or
'            an open ADO Connection object.
'   RelURI is the relative URI to an item within the store
'
Function getRecord_ro( relURL, Conn )

 If not VarType(relUrl) = vbString And ( (TypeName(Conn) = "Connection") Or (TypeName(Conn) = "Nothing") ) Then
  Err.Raise &H80070057 ' E_INVALIDARG
 End If

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

 If Not TypeName(vSource) = "Connection" Then
  Set Conn = getConnection(relUrl)
 End If

 Rec.Open relURL, Conn

 Set getRecord_ro = Rec

End Function

' getRecord_rw
'   vSource is either the name of a top-level public folder or
'           an open ADO Connection object.
'   RelURI is the relative URI to an item within the store
'
Function getRecord_rw(relURL, Conn )

 If not VarType(relUrl) = vbString And ( (TypeName(Conn) = "Connection") Or (TypeName(Conn) = "Nothing") ) Then
  Err.Raise &H80070057 ' E_INVALIDARG
 End If

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

 If Not TypeName(Conn) = "Connection" Then
  Set Conn = getConnection(relUrl)
 End If

 Rec.Open relURL, Conn, adModeReadWrite
 Set getRecord_rw = Rec

End Function

Assume that you have a folder called "test1" below your Public Folders top-level folder in a public store, and an item in "test1" called "item1.txt". To access the folder and the item, you could use these functions in the following way:

VBScript

Example

Const adDefaultStream = -1

Dim Conn
Dim Rec
Dim Rec2

Set Conn = getConnection("/apps")
Set Rec  = getRecord_rw("/apps/test1/Schema",    Conn)
Set Rec2 = getRecord_ro("/apps/test1/item1.txt", Conn)

WScript.Echo Rec.Fields("DAV:href")
Wscript.Echo Rec.Fields("DAV:contentclass")

wscript.echo

WScript.Echo Rec2.Fields("DAV:href")
wscript.echo Rec2.fields("DAV:contentclass")
Set Stm = Rec2.Fields(adDefaultStream).Value
stm.charset = "us-ascii"
WScript.Echo Stm.readText

Note that the string "Public Folders" does not appear in the relative URL. This name is used to connect to the appropriate public folder tree. After you have connected, you no longer need to specify this information in your relative URLs.