Platform SDK: Active Directory, ADSI, and Directory Services

The Get Method

Use the IADs::Get function to retrieve individual properties from the directory object. The following example uses IADs::Get to retrieve a property by name from an object.

Dim MyUser as IADs
Dim MyCommonName as String
 
' Bind to a specific user object.
set MyUser = GetObject("LDAP://CN=JamesSmith,OU=MyOrgUnit")
 
' Get property
MyCommonName = MyUser.Get("CN")

From Automation languages, you can use the property name directly using the dot notation. For example, you can write MyObject.Name or MyObject.Class to access the Name and Class properties defined on IADs. The following example retrieves a property directly from an Active Directory object using the standard IADs interface and the Name property it supports.

Dim MyUser as IADs
Dim MyName as String
 
' Bind to a specific user object.
Set MyUser = GetObject("LDAP://MyMachine/CN=JamesSmith,DC=Microsoft,DC=COM")
 
' Get property
MyName = MyUser.Name

You can also use the name of the schema object that describes the property. Pass the name of the property as it is defined in the schema as the first parameter.

Dim MyUser as IADs
Dim MyName as String
 
' Bind to a specific user object.
set MyUser = GetObject("LDAP://CN=JamesSmith,OU=MyOrgUnit")
 
' Get property
MyName = MyUser.Get("distinguishedName")

IADs::Get can return either a simple variant or an array of variants with multiple values, depending upon the attribute values returned by the directory server.

Example:
'--- An attribute with a single value–
Set usr = GetObject("LDAP://CN=jsmith, OU=Sales, DC=Microsoft, DC=Com")
usrname = usr.Get("samAccountName")
 
'---An attribute with multiple values---
Set usr = GetObject("LDAP://CN=jsmith, OU=Sales, DC=Microsoft, DC=COM")
homePhones = usr.Get("otherHomePhone")
if ( VarType(homePhones) = vbString Then
    Debug.Print homePhones
Else
    For each phone in homePhones
         Debug.Print phone
    Next
End if
 
'--- Attribute which turns into a COM object ---
Set ou  = GetObject("LDAP://OU=Sales, DC=Microsoft, DC=Com")
Set sd  = ou.Get("ntSecurityDescriptor")
Set acl = sd.DiscretionaryACL

From languages that do not use Automation, you can also use the IADs::Get method and its related methods as described previously. In addition, you can use the get_propertyname methods supported on all ADSI interfaces.

IADs       *pUser; 
BSTR       bstrName;
HRESULT    hr;
 
// Bind to user object
hr = ADsGetObject(L"WinNT://MyDomain/Users/James", IID_IADs, (void**)&pUser);
 
// Get property
if (SUCCEEDED(hr)) {
    hr = pUser->get_Name(&bstrName);
 
    if (SUCCEEDED(hr)) {
        printf("%S\n", bstrName);
    }
 
    SysFreeString(bstrName)
}