Assigning multiple properties to "directReports" using the Active Directory APIs System.DirectoryServices

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; //get it from somewhere
de.Properties["directReports"].Value = object[] { "CN=user123,CN=Users,DC=DOMAIN,DC=xyz", "dn2", "dn3" };
de.CommitChanges(); //error: contraint violation occurred

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)
        {
            //foreach(var v in 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 #?

+1
1

, directReports - Active Directory.

10 , directReports 10 .

- employee manager, manager directReports Active Directory

+1

All Articles