Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

alt text

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-

like image 459
Eric Brown - Cal Avatar asked Jan 22 '23 05:01

Eric Brown - Cal


2 Answers

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();
                }
            }
        }
    }
like image 78
Sikindar Avatar answered Feb 09 '23 16:02

Sikindar


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....

like image 37
marc_s Avatar answered Feb 09 '23 14:02

marc_s