Directory Services

Creating Groups

This topic shows how to create several types of groups.

The following code example shows how to create a new group, called Practice Managers to the organizational unit, called Consulting.

[C#]
DirectoryEntry dom = new DirectoryEntry();
DirectoryEntry ou = dom.Children.Find("OU=Consulting");
// If group type is not specified, the default is to create a global, secured group.
DirectoryEntry group = ou.Children.Add("CN=Practice Managers","group");
group.CommitChanges();

The following code example shows how to create a local domain group called Managers to the Consulting organizational unit. Use COM Interop to specify the ADS_GROUP_TYPE_DOMAIN_LOCAL flag.

[C#]
DirectoryEntry dom = new DirectoryEntry();
DirectoryEntry ou = dom.Children.Find("OU=Consulting");
DirectoryEntry mgr = ou.Children.Add("CN=Managers","group");
mgr.Properties["groupType"].Value = ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP | 
ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED;
mgr.CommitChanges();

The following code example shows how create a non-security group, called Full Time Managers to the Consulting organizational unit. Use COM Interop to specify the ADS_GROUP_TYPE_GLOBAL_GROUP flag.

[C#]
DirectoryEntry dom = new DirectoryEntry();
DirectoryEntry ou = dom.Children.Find("OU=Consulting");
DirectoryEntry dl = ou.Children.Add("CN=Full Time Employees","group");
dl.Properties["groupType"].Value = ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP;
dl.CommitChanges();

The following code example shows how to add an entire group to another group.

[C#]
DirectoryEntry dom = new DirectoryEntry();
DirectoryEntry ou = dom.Children.Find("OU=Consulting");
// Add Group to a group. 
mgr.Properties["member"].Add(group.Properties["distinguishedName"].Value);
mgr.CommitChanges();