how do i create new OU in Active Directory using DirectoryServices.AccountManagement in .net 3.5 or 4
Asked Answered
S

1

6

to create & find users & groups in Active Directory i've been using this code: http://anyrest.wordpress.com/2010/06/28/active-directory-c/ that is using the new System.DirectoryServices.AccountManagement namespace that was introduced in .net 3.5...

i'd like to add a method that creates a new OU (if the OU doesnt exist already) using the newest technology with .net 3.5 or 4.0 (and not using the old System.DirectoryServices)

any idea how to do that ?

Saltcellar answered 17/10, 2011 at 10:49 Comment(0)
H
10

According to Managing Directory Security Principals in the .NET Framework 3.5 specialy the architecture here under and System.DirectoryServices.AccountManagement Namespace article, accountManagement is for users groups and computers (security principals).

Active Directory Architecture

For organizationalUnit, you can use System.DirectoryServices.ActiveDirectory here is an example :

using System.DirectoryServices;

...

/* Connection to Active Directory
 */
DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/ou=Monou,dc=dom,dc=fr", "jpb", "PWD");

DirectorySearcher ouSrc = new DirectorySearcher(deBase);
ouSrc.Filter = "(OU=TheNewOU)";
ouSrc.SearchScope = SearchScope.Subtree;
SearchResult srOU = ouSrc.FindOne();
if (srOU == null)
{
  /* OU Creation
   */
  DirectoryEntry anOU = deBase.Children.Add("OU=TheNewOU", "organizationalUnit");
  anOU.Properties["description"].Value = "The description you want";
  anOU.CommitChanges();
}

Don't forget to use using(){} directive

Hammer answered 17/10, 2011 at 11:30 Comment(2)
I think you can use the PrincipalContext to retreive the Container value.Hammer
but can i check if the OU exists ? and can i add a new OU ? if not, i do i take the principal context and create a DirectoryEntery object from it's properties?Saltcellar

© 2022 - 2024 — McMap. All rights reserved.