Adding Directory Objects

To add new directory objects, use the Children property of the DirectoryEntry class. The Children property returns a DirectoryEntries object that exposes the Add method.

To add an object, bind to the container that the object is to be added to and, after adding the object, call CommitChanges to save the object from the cache to the directory. The following code example shows how to use the Add method to add new objects.

[Visual Basic .NET]

Try
    ' Bind to the Users container, add a new group and a new contact.
    Dim de As New DirectoryEntry("LDAP://CN=Users,DC=fabrikam,DC=com")
    Dim newGroup As DirectoryEntry = de.Children.Add("CN=Sales", "group")
    newGroup.CommitChanges()
    Dim newContact As DirectoryEntry = de.Children.Add("CN=New Contact", "contact")
    newContact.CommitChanges()
    ' Bind to the Computers container and add a new computer.
    Dim de01 As New DirectoryEntry("LDAP://CN=Computers,DC=fabrikam,DC=com")
    Dim newComputer As DirectoryEntry = de01.Children.Add("CN=New Computer", "computer")
    newComputer.CommitChanges()
Catch Exception1 As Exception
        ' If a COMException is thrown, then the following code can catch the text of the error.
        ' For more information about handling COM exceptions, see Handling Errors.
        Dim COMEx As System.Runtime.InteropServices.COMException = CType(Exception1, System.Runtime.InteropServices.COMException)
        Console.WriteLine(COMEx.ErrorCode)
End Try

[C#]

try
{
    // Bind to the Users container, add a new group and a new contact.
    DirectoryEntry de = new DirectoryEntry("LDAP://CN=Users,DC=fabrikam,DC=com");
    DirectoryEntry newGroup = de.Children.Add("CN=Sales", "group");
    newGroup.CommitChanges();
    DirectoryEntry newContact = de.Children.Add("CN=New Contact", "contact");
    newContact.CommitChanges();
    // Bind to the Computers container and add a new computer.
    DirectoryEntry de01 = new DirectoryEntry("LDAP://CN=Computers,DC=fabrikam,DC=com");
    DirectoryEntry newComputer = de01.Children.Add("CN=New Computer", "computer");
    newGroup.CommitChanges();
}
catch (Exception Exception1)
{
    // If a COMException is thrown, then the following code example can catch the text of the error.
    // For more information about handling COM exceptions, see Handling Errors.
    System.Runtime.InteropServices.COMException COMEx = 
       (System.Runtime.InteropServices.COMException) Exception1;
    Console.WriteLine(COMEx.ErrorCode);
}

After adding a new object, use the Exists method to verify an entry in the directory. This method is provided in the DirectoryEntry class. The following code example shows how to use Exists.

[Visual Basic .NET]

If ds.Exists("LDAP://CN=Sales,CN=Users,DC=fabrikam,DC=com") = True Then
   Console.WriteLine("object exists")
Else
   Console.WriteLine("object does not exist")
End If

[C#]

if (DirectoryEntry.Exists("LDAP://CN=Sales,CN=Users,DC=fabrikam,DC=com"))
    Console.WriteLine("object exists");
else
    Console.WriteLine("object does not exist");