In c# how do I set the active directy field "office" so i can show the location of our users in Outlook
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?
开发者_如何学JAVAThanks,
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....
精彩评论