2
votes

Currently working on automating creation of Active Directory users, but I cannot figure out how to update the field indicated in the linked image. (I can't attach images because I have fewer than 10 posts) image

I can update the user logon name by (userprincipal being an instance of the userprincipal object set to the appropriate user):

userPrincipal.UserPrincipalName = logonName

and I can update other similar user properties such as job title by (user location being an instance of directoryentry that points to the directory location of the user):

(userLocation.Properties["title"]).Value = title;

But no matter what I try and adjust, I haven't been able to figure out how to update that particular field. Any help is greatly appreciated!

1

1 Answers

0
votes

You must know the property name before you can set it. there can be 100s and 100s of properties in the active directory some being set from the user interface and some being set from somewhere else, whatever you can check for the properties name by doing the following

DirectoryEntry directoryEntry = new DirectoryEntry(ConnectionString, ProviderUserName, ProviderPassword, AuthenticationTypes.Secure);
 /******************************/

 DirectorySearcher search = new DirectorySearcher(directoryEntry);
 search.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
 search.CacheResults = false;

 SearchResultCollection allResults = search.FindAll();
 StringBuilder sb = new StringBuilder();

 foreach (SearchResult searchResult in allResults)
 {
     foreach (string propName in searchResult.Properties.PropertyNames)
     {
         ResultPropertyValueCollection valueCollection = searchResult.Properties[propName];
         foreach (Object propertyValue in valueCollection)
         {
             sb.AppendLine(string.Format("property:{0}, value{1}<br />", propName, propertyValue));
         }
     }
 }

 return sb.ToString();

this will result in all the active directory for some user you provide

after that you will be able to set all the properties that you want because you have the property name which is available for the user