In c# I'm trying to set the office field
When I do this:
ADEntry.Properties[ "office"].Add( "Alaska");
It says office does not exist.
Can anyone tell me where to get at this property?
Thanks,
Cal-
After long research I got it..
string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(System.Security.Principal.WindowsIdentity.GetCurrent().Name.IndexOf("\\") + 1);
string office = string.Empty;
using (var context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["DOMAIN"].ToString()))
{
using (var userPrincipal = new UserPrincipal(context))
{
userPrincipal.SamAccountName = Username;
using (PrincipalSearcher search = new PrincipalSearcher(userPrincipal))
{
UserPrincipal result = (UserPrincipal)search.FindOne();
DirectoryEntry directoryEntry = result.GetUnderlyingObject() as DirectoryEntry;
if (directoryEntry.Properties["physicalDeliveryOfficeName"].Count > 0
&& directoryEntry.Properties["physicalDeliveryOfficeName"][0] != null
&& !string.IsNullOrWhiteSpace(directoryEntry.Properties["physicalDeliveryOfficeName"][0].ToString()))
{
office = directoryEntry.Properties["physicalDeliveryOfficeName"][0].ToString();
}
}
}
}
Check out Richard Mueller's web site - he has tons of reference Excel sheets on what property in the AD UI maps to what underlying AD property on DirectoryEntry
.
Your concrete "office" example maps to a property called physicalDeliveryOfficeName
in the DirectoryEntry
's .Properties
collection....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With