views:

65

answers:

3

I'm creating and updating Groups in Active Directory using the GroupPrincipal class in System.DirectoryServices.AccountManagement. When creating and updating, I also need to be able to set the ManagedBy property that you are able to set in the Managed By tab in the groups properties in the AD management console.

Can it be done programatically?

A: 

Take a look at this page. This is one of the best tutorials on AD in c#.

Some code that should work(untested) :

    string connectionPrefix = "LDAP://" + ouPath;
    DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
    DirectoryEntry newGroup = dirEntry.Children.Add
        ("CN=" + groupName, "group");
    group.Properties["sAmAccountName"].Value = groupName;
    newGroup.Properties["managedBy"].Value = managerDistinguishedName;
    newGroup.CommitChanges();
    dirEntry.Close();
    newGroup.Close();
apoorv020
That's not using the System.DirectoryServices.AccountManagement namespace that the OP requested...
marc_s
hmmm... missed that. (Was that an edit?)
apoorv020
+1  A: 

You could extend the GroupPrincipal class and provide a ManagedBy property using the ExtensionSet method.

Paolo Tedesco
This is how I'm attacking the problem... though I'm having trouble making `ExtensionSet` work properly
James B
A: 

You cannot do this directly, unfortunately - but you can get access to the underlying DirectoryEntry and do it there:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

UserPrincipal toBeModified = UserPrincipal.FindByIdentity(".....");
UserPrincipal manager = UserPrincipal.FindByIdentity(ctx, "......");

DirectoryEntry de = toBeModified.GetUnderlyingObject() as DirectoryEntry;

if (de != null)
{
    de.Properties["managedBy"].Value = manager.DistinguishedName;
    toBeModified.Save();
}

I can't test it right now - but I think this should work ok.

marc_s
awesome, thanks this seemed to work!
michael_erasmus