This topic is locked
[SOLVED]

 Add a group to Active Directory

8/12/2014 4:06:00 PM
ASPRunner.NET General questions
T
Tim author

In the "Before record added" event on one of the pages of my application, I would like to use c# code to add a group to Active Directory. Eventually I will use fields from the form to get the name of the group to be created, but I thought I would start by just using hard-coded info. I found this page at Microsoft: Creating Groups
I copied the example and changed to match my environment. Here is the code I used:
// Bind to the domain that this user is currently connected to.

DirectoryEntry dom = new DirectoryEntry();
// Find the container (in this case, the Consulting organizational unit) that you

// wish to add the new group to.

DirectoryEntry ou = dom.Children.Find("OU=MyOU");
// Add the new group Practice Managers.

DirectoryEntry group = ou.Children.Add("CN=Practice Managers", "group");
// Set the samAccountName for the new group.

group.Properties["samAccountName"].Value = "pracmans";
// Commit the new group to the directory.

group.CommitChanges();
But when I build the application I get the following error log when compiling:

error CS0246: The type or namespace name 'DirectoryEntry' could not be found (are you missing a using directive or an assembly reference?)
Is there a way to make this work? Or could you point me to some info on how I can achieve this?
Thanks,

Tim

admin 8/15/2014

I guess you need to add a reference to System.DirectoryServices.dll (More info: http://stackoverflow.com/questions/17314040/directoryentry-is-not-getting-even-add-the-namespace-system-directoryservices).
You will have to add this reference to your project using Visual Studio and compile project right there.