Example: Constructing a Distinguished Name

When an attribute value change to the metaverse occurs, such as changing the employee status, you might have to change the distinguished name of an existing connector space entry to rename or move the object in a connected data source.

You can change the distinguished name of an existing connector by first iterating through the ConnectorCollectionByIndex.Item property to get the first connector. If the metaverse entry contains multiple connectors, use conditional logic to identify the appropriate connector. After getting the appropriate connector, set the CSEntry.DN property to the new distinguished name.

The following example shows how to construct a distinguished name using the ReferenceValue.Concat method. If the distinguished name exists, this example catches the exception and appends an integer to the end of the distinguished name until the distinguished name is unique.

    Public Sub Provision(ByVal mventry As MVEntry) _
        Implements IMVSynchronization.Provision
    
        Dim uniqueDN As Boolean = False
        Dim connectors As Integer
        Dim index As Integer = 0
        Dim csEntry As CSEntry
        Dim container, rdn As String
        Dim ma As ConnectedMA
        Dim dn As ReferenceValue
    
        Dim ExceptionMessage As String
    
        container = "CN=users,DC=fabrikam,DC=com"
        ma = mventry.ConnectedMAs("Fabrikam AD MA")
    
        If Not mventry("cn").IsPresent Then
    
            ExceptionMessage = "The attribute cn was unexpectedly not present on the metaverse object."
            Throw New UnexpectedDataException(ExceptionMessage)
    
        Else
            rdn = "CN=" & mventry("cn").Value
            dn = ma.EscapeDNComponent(rdn).Concat(container)
            connectors = ma.Connectors.Count
    
            ' If there are no existing connectors, create a new connector.
            If 0 = connectors Then
    
                ' Verify whether the distinguished name is unique. If it
                ' is not unique, create a new distinguished name.
                While Not uniqueDN
                    Try
                        csEntry = ma.Connectors.StartNewConnector("user")
                        csEntry.DN = dn
    
                        csEntry.CommitNewConnector()
                        uniqueDN = True
    
                    ' The catch block creates a new distinguished name by
                    ' appending an integer to the original rdn (cnValue).
                    Catch oaex As ObjectAlreadyExistsException
                        rdn = "CN=" + mventry("cn").Value + index.ToString()
                        dn = ma.EscapeDNComponent(rdn).Concat(container)
                        index = index + 1
                        uniqueDN = False
                    End Try
    
                End While
    
            End If
    
        End If
    End Sub
    void IMVSynchronization.Provision (MVEntry mventry)
    {
        bool uniqueDN = false;
        int connectors, index = 0;
        CSEntry csEntry;
        string container, rdn;
        ConnectedMA ma;
        ReferenceValue dn;
    
        container = "CN=users,DC=fabrikam,DC=com";
        ma        = mventry.ConnectedMAs["Fabrikam AD MA"];
    
        if(!mventry["cn"].IsPresent)
        {
            string ExceptionMessage;
            ExceptionMessage = "The attribute cn was unexpectedly not present on the metaverse object.";
            throw new UnexpectedDataException(ExceptionMessage);
        }
        
        else
        {    
            rdn         = "CN=" + mventry["cn"].Value;
            dn          = ma.EscapeDNComponent(rdn).Concat(container);
            connectors  = ma.Connectors.Count;
    
            // If there are no existing connectors, create a new connector.
            if(0 == connectors)
            {
                // Verify whether the distinguished name is unique. If it
                // is not unique, create a new distinguished name.
                while(!uniqueDN)
                {
                    try
                    {
                        csEntry    = ma.Connectors.StartNewConnector("user");
                        csEntry.DN = dn;               
    
                        // If connector space entry with the same distinguished name exists,
                        // CommitNewConnector() throws an exception.
                        csEntry.CommitNewConnector();
                        uniqueDN = true;
                    }
                    
                    // The catch block creates a new distinguished name by
                    // appending an integer to the original rdn (cnValue).
                    catch(ObjectAlreadyExistsException)
                    {
                        rdn = "CN=" + mventry["cn"].Value + index.ToString();
                        dn  = ma.EscapeDNComponent(rdn).Concat(container);
                        index++;
                        uniqueDN = false;
                    }
                }
            }        
        }
    }
  • Make sure that you have all of the required information to construct the new distinguished name prior to calling the ManagementAgent.EscapeDNComponent method.
  • If you attempt to rename a connector by changing the value of the CSEntry.DN property and the new value differs from the original value by case, the rename attempt is ignored because the new value is really the same as the original value. However, if you attempt to rename a connecter by changing the value of the CSEntry.RDN property, the rename occurs even if the original and new values for the CSEntry.DN property differ only in case. If the original and new values for the CSEntry.DN property are the same, the rename attempt is ignored.

See Also

CSEntry.DN
CSEntry.RDN
ReferenceValue.Concat

Send comments about this topic to Microsoft

Build date: 2/16/2009