Trolling for Properties

Previously I've described using the Get and GetEx methods of the IADs interface to return individual properties by name. Active Directory, however, is a dynamic database with an extensible schema. This means that new properties can be defined for any object in the directory. You might not know the names of these properties or the types of values they contain, but you might need to examine them nonetheless. Utility applications that browse Active Directory need this capability in particular.

The properties of an Active Directory object fall into two categories: The properties that can be contained within an object, and the properties that are contained within an object. Of course, the latter group will be a subset of the former group. First I'll cover those properties which are available to an object, and then I'll discuss how to manipulate the properties and values currently associated with a particular object.

One way to determine which properties can be contained within an object is to examine the object's class definition in the Active Directory schema. The object's class defines the set of properties the object could contain, not those that it actually does contain. Listing 7-1 shows code from the ClassProperties.bas sample on the companion CD. This sample uses the Schema property of the IADs interface to return an ADsPath to the schema object for the class of the object we're examining. The code enumerates the list of mandatory and optional properties of the domainDNS class, displaying the value of each if it exists within the object. Since I'm using the Get method, each existing property value is returned as a variant. If a property doesn't exist, the On Error Resume Next statement keeps the program moving to the next possible property.

 Public Sub Main()
` Define which ADSI interfaces to use
Dim objRootDSE As IADs
Dim objADs As IADs
Dim strValue As String
Dim strADsPath as String
Dim varProperty as Variant
` Connect to the LDAP server's root object
Set objRootDSE = GetObject("LDAP://RootDSE")
` Form a path to the root domain object
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext")
` Bind to the object
Set objADs = GetObject(strADsPath)
` Get the schema class for the object
Dim objADsSchemaClass As IADsClass
Set objADsSchemaClass = GetObject(objADs.Schema)
` Display object information
Debug.Print "Name:   " & objADs.Name
Debug.Print "Class:  " & objADs.Class ` Enumerate all the mandatory properties for this class
Debug.Print "---Mandatory Properties---"
For Each varProperty In objADsSchemaClass.MandatoryProperties
    ` Display property name and value for the object
    Debug.Print vbTab & varProperty & ": ";
    
    ` Trap errors since not all attributes will be
    ` contained in a particular instance of the class
    On Error Resume Next
    
    ` Display the current value for this property
    strValue = objADs.Get(varProperty)
    
    If Err.Number = 0 Then
        Debug.Print strValue
    Else
        Debug.Print
    End If
    
    ` Turn off error checking
    On Error GoTo 0
Next
` Enumerate all the optional properties
Debug.Print "---Optional properties---"
For Each varProperty In objADsSchemaClass.OptionalProperties
    ` Display property name and value for the object
    Debug.Print vbTab & varProperty & ": ";
    
    ` Trap errors since not all attributes will be
    ` contained in a particular instance of the class
    On Error Resume Next
    ` Display the current value for this property
    strValue = objADs.Get(varProperty)
    
    If Err.Number = 0 Then
        Debug.Print strValue
    Else
        Debug.Print
    End If
    
    ` Turn off error checking
    On Error GoTo 0
Next
End Sub

Listing 7-1 ClassProperties.bas enumerates all properties of the domainDNS class.

When you run the code in Listing 7-1, you'll see a long list of mandatory and optional properties for the domainDNS class. The following shows an abbreviated sample output. The exact values will vary from server to server.

 Name:   DC=coppersoftware
Class:  domainDNS
---Mandatory Properties---
    dc: coppersoftware
    instanceType: 5
    nTSecurityDescriptor: 
    objectCategory: CN=Domain-DNS,CN=Schema,CN=Configuration,
        DC=coppersoftware,DC=com
    objectClass: 
---Optional properties---
    adminDescription: 
    adminDisplayName: 
    allowedAttributes: 
    allowedAttributesEffective: 
    allowedChildClasses: 
    ...
    whenChanged: 1/1/2001 9:34:00 AM
    whenCreated: 1/1/2001 9:27:20 AM
    wWWHomePage: 

You can use the IADs methods to access and manipulate the properties of an object, but you must know the property names. In the sample shown in Listing 7-1, the names of the properties are provided by the schema class of the object, whether or not a value is assigned to the property. To provide more flexibility when working with properties in the cache, some of which might be unknown to you, ADSI supplies the IADsPropertyXXX family of interfaces, also known as the property cache interfaces. These interfaces enable a program to browse the local property cache and determine the values of each object property.



MicrosoftR WindowsR 2000 Active DirectoryT Programming
MicrosoftR WindowsR 2000 Active DirectoryT Programming
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 108

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net