Link to home
Start Free TrialLog in
Avatar of KazooSoft
KazooSoftFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Setting extensionAttribute1 using DirectoryEntry c#

Morning all,

I have been creating accounts using the DirectoryEntry in c# but now I am required to set extensionAttributes but when I try the code below I get errors saying "The attribute syntax specified to the directory service is invalid"

How can i set the extensionAttributes?


/// <summary>
        /// Create a new user account in the Active Directory
        /// Function by http://www.Kazoosoft.eu
        /// copyright Steven Church steven@kazoosoft.eu
        /// </summary>
        /// <param name="ldapPath"></param>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <param name="userPrincipalName"></param>
        /// <param name="GivenName"></param>
        /// <param name="SN"></param>
        /// <param name="profilePath"></param>
        /// <param name="homeDirectory"></param>
        /// <param name="mail"></param>
        /// <param name="mailNickname"></param>
        /// <param name="distinguishedName"></param>
        /// <param name="displayName"></param>
        /// <param name="scriptPath"></param>
        /// <param name="homeDrive"></param>
        /// <param name="extensionAttribute1"></param>
        /// <param name="extensionAttribute2"></param>
        /// <param name="extensionAttribute3"></param>
        /// <param name="extensionAttribute4"></param>
        /// <param name="extensionAttribute5"></param>
        /// <param name="extensionAttribute6"></param>
        /// <param name="extensionAttribute7"></param>
        /// <param name="extensionAttribute8"></param>
        /// <param name="extensionAttribute9"></param>
        /// <param name="extensionAttribute10"></param>
        /// <param name="extensionAttribute11"></param>
        /// <param name="extensionAttribute12"></param>
        /// <returns></returns>
        public static string CreateUserAccount(string ldapPath,
            string userName, 
            string userPassword, 
            string userPrincipalName, 
            string GivenName,
            string SN,
            string profilePath, 
            string homeDirectory, 
            string mail, 
            string mailNickname, 
            string distinguishedName, 
            string displayName,
            string scriptPath,
            string homeDrive,
            string extensionAttribute1,
            string extensionAttribute2,
            string extensionAttribute3,
            string extensionAttribute4,
            string extensionAttribute5,
            string extensionAttribute6,
            string extensionAttribute7,
            string extensionAttribute8,
            string extensionAttribute9,
            string extensionAttribute10,
            string extensionAttribute11,
            string extensionAttribute12)
            //string accountExpires, 
            //string proxyAddress
            
        {
            try
            {
                string oGUID = string.Empty;
                string connectionPrefix = "LDAP://" + ldapPath;
                //string connectionPrefix = "LDAP://IPADDRESS/OU=2010,OU=Stu,OU=Test_Area,OU=Group Test,DC=domain,DC=local";
                DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix,
                    KazooADIntegration.Properties.Settings.Default.LDAP_domain + "\\" + KazooADIntegration.Properties.Settings.Default.LDAP_username,
                    KazooADIntegration.Properties.Settings.Default.LDAP_password,
                    AuthenticationTypes.Secure);

                //DirectoryEntry dirEntry = new DirectoryEntry();
                DirectoryEntry newUser = dirEntry.Children.Add
                    ("CN=" + userName, "user");

                newUser.Properties["sAMAccountName"].Add(userName);            
                newUser.Properties["userPrincipalName"].Add(userPrincipalName);
                newUser.Properties["givenName"].Add(GivenName);
                newUser.Properties["sn"].Add(SN);
                newUser.Properties["profilePath"].Add(profilePath);
                newUser.Properties["homeDirectory"].Add(homeDirectory);
                newUser.Properties["mail"].Add(mail);
                newUser.Properties["mailNickname"].Add(mailNickname);
                newUser.Properties["distinguishedName"].Add(distinguishedName);
                newUser.Properties["displayName"].Add(displayName);
                newUser.Properties["scriptPath"].Add(scriptPath);
                newUser.Properties["homeDrive"].Add(homeDrive);

                
                // Set the Extension Attributes
                if (extensionAttribute1 != null)
                {
                    newUser.Properties["extensionAttribute1"].Add(extensionAttribute1);
                }
                if (extensionAttribute2 != null)
                {
                    newUser.Properties["extensionAttribute2"].Add(extensionAttribute2);
                }
                if (extensionAttribute3 != null)
                {
                    newUser.Properties["extensionAttribute3"].Add(extensionAttribute3);
                }
                if (extensionAttribute4 != null)
                {
                    newUser.Properties["extensionAttribute4"].Add(extensionAttribute4);
                }
                if (extensionAttribute5 != null)
                {
                    newUser.Properties["extensionAttribute5"].Add(extensionAttribute5);
                }
                if (extensionAttribute6 != null)
                {
                    newUser.Properties["extensionAttribute6"].Add(extensionAttribute6);
                }
                if (extensionAttribute7 != null)
                {
                    newUser.Properties["extensionAttribute7"].Add(extensionAttribute7);
                }
                if (extensionAttribute8 != null)
                {
                    newUser.Properties["extensionAttribute8"].Add(extensionAttribute8);
                }
                if (extensionAttribute9 != null)
                {
                    newUser.Properties["extensionAttribute9"].Add(extensionAttribute9);
                }
                if (extensionAttribute10 != null)
                {
                    newUser.Properties["extensionAttribute10"].Add(extensionAttribute10);
                }
                if (extensionAttribute11 != null)
                {
                    newUser.Properties["extensionAttribute11"].Add(extensionAttribute11);
                }
                if (extensionAttribute12 != null)
                {
                    newUser.Properties["extensionAttribute12"].Add(extensionAttribute12);
                }
                
                

                newUser.CommitChanges();
                
                oGUID = newUser.Guid.ToString();

                newUser.Invoke("SetPassword", new object[] { userPassword });
                newUser.CommitChanges();
                dirEntry.Close();
                newUser.Close();
                return "created";

                
            }

Open in new window

Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi KazooSoft,

It would be worth verifying the entry it's attempting to add if possible?

I guess none of the values passed as extensionAttribute* are empty ("")? I don't think you'd catch that with the Null test, and AD won't like you for attempting it (the error you have is exactly what I'd expect to see).

Perhaps you might use:

if (!String.IsNullOrEmpty(extensionAttribute1)) {
  // Add the property
}

HTH

Chris
ASKER CERTIFIED SOLUTION
Avatar of jdavistx
jdavistx

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial