My initial attempts to assign multiple values โโas directReports to a user in Active Directory were to use DirectoryEntry objects and assign them as follows:
DirectoryEntry de;
de.Properties["directReports"].Value = object[] { "CN=user123,CN=Users,DC=DOMAIN,DC=xyz", "dn2", "dn3" };
de.CommitChanges();
It also did not work for the "manager" attribute.
Then I started using UserPrincipal extension methods (which use DirectoryEntries in the background, right?)
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
public UserPrincipalEx(PrincipalContext pc)
: base(pc)
{
}
public void SetManager(string value)
{
this.ExtensionSet("manager", value);
}
public void SetDirectReports(string[] values)
{
this.ExtensionSet("directReports", values);
}
public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
{
return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
}
public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue);
}
}
Assigning a manager by sending a highlighted name string works fine, but it does not work for direct reports. I'm still gettingInvalidOperationException: A constraint violation occurred.
I try to call it this way:
PrincipalContext pc = new PrincipalContext(ContextType.Domain, "mazen", user, pass);
UserPrincipalEx up = UserPrincipalEx.FindByIdentity(pc, IdentityType.SamAccountName, "moses");
var dns = new string[] { "CN=someone,CN=Users,DC=DOMAIN,DC=xyz", "CN=anotherone,CN=Users,DC=DOMAIN,DC=xyz" };
up.SetDirectReports(dns);
How to assign property of multiple values โโof direct reports using C #?