Team LiB
Previous Section Next Section

Writing New Objects to Active Directory

Using the DirectoryEntries class and collection, you can add objects to the Active Directory. This following example demonstrates creating a new User object in the Active Directory.

Caution 

When writing new objects to Active Directory, it is important to remember that you are working in the client's memory and that the changes are not propagated to the server until you call CommitChanges.

Code Example: Writing Objects to Active Directory
Start example
using System;
using System.DirectoryServices;

namespace Chapter12
{
          public class AddingObjectsToTheDirectory
          {
                  public AddingObjectsToTheDirectory()
                  {
                         DirectoryEntry MyObject = new DirectoryEntry();
                         MyObject.Path =
                            "LDAP://HMSRevenge/OU=Users,DC=Test,DC=com";
                         DirectoryEntries users = MyObject.Children;
                        //Creates a new user object
                         DirectoryEntry NewUser =
                            users.Add("Greg MacBeth", "user");
                        //Modifies the properties of an object
                         NewUser.Properties["company"].Add
                            ("Microsoft Corporation");
                         NewUser.Properties["employeeID"].Add("1001");
                         NewUser.Properties["userPassword"].Add("Password");
                         NewUser.CommitChanges();
                  }
         }
}
End example

Team LiB
Previous Section Next Section