Share via


Adding Users to a Group

When a group is created, users must be added to the group. This topic describes how to add a user to a group.

The following example shows how add a user or a new user to the consulting organization.

[C#]

DirectoryEntry dom = new DirectoryEntry();
DirectoryEntry group = dom.Children.Find("CN=Consulting");
// Add a single user to a group;
DirectoryEntry usr = group.Children.Find("CN=New User");

// To add multiple users to a group use 
//group.Properties["member"].AddRange(new string[] {"userDN1","userDN2"});

//To add the user's distinguished name to the member property
//on the group object, use the Add method.
group.Properties["member"].Add(usr.Properties["distinguishedName"].Value);
//Commit the changes to the directory.
group.CommitChanges();

[Visual Basic .NET]

Dim dom As New DirectoryEntry()
Dim group As DirectoryEntry = dom.Children.Find("CN=Consulting")
' Add a single user to a group;
Dim usr As DirectoryEntry = group.Children.Find("CN=New User")

' To add multiple users to a group use 
'group.Properties("member").AddRange(New String() {"userDN1", "userDN2"})

'To add the user's distinguished name to the member property
'on the group object, use the Add method.
group.Properties("member").Add(usr.Properties("distinguishedName").Value)
'Commit the changes to the directory.
group.CommitChanges()

Note  These examples require the .NET Framework version 1.0 SP3 and later or the .NET Framework version 1.1 SP1 and later.