Search code examples
c#active-directoryldapdirectoryentry

Unable to read Mobile # field from Active Directory


I am trying to connect to the Active Directory (2003) to update the Mobile # field which appears in the OutLook address book as in the attached image below.

enter image description here

I am able to read most of the fields using the code below, but the otherTelephone, mobile, otherMobile fields are not found. What is the reason?

static void Main(string[] args)
    {
        Console.Write("Enter user      : ");
        String username = Console.ReadLine();

        try
        {
            DirectoryEntry myLdapConnection = createDirectoryEntry();

            DirectorySearcher search = new DirectorySearcher(myLdapConnection,);
            search.Filter = "(sAMAccountName=" + username + ")";
            search.PropertiesToLoad.Add("title");
            search.PropertiesToLoad.Add("street");
            search.PropertiesToLoad.Add("department");
            search.PropertiesToLoad.Add("mail");
            search.PropertiesToLoad.Add("manager");
            search.PropertiesToLoad.Add("telephoneNumber");
            search.PropertiesToLoad.Add("otherTelephone");
            search.PropertiesToLoad.Add("mobile");
            search.PropertiesToLoad.Add("otherMobile");


            SearchResult result = search.FindOne();

            if (result != null)
            {
                DirectoryEntry entryToUpdate = result.GetDirectoryEntry();
                Console.WriteLine("Current title   : " + entryToUpdate.Properties["title"][0].ToString());

                //Console.Write("\n\nEnter new title : ");
                //String newTitle = Console.ReadLine();
                //entryToUpdate.Properties["title"].Value = newTitle;
                //entryToUpdate.CommitChanges();
                //Console.WriteLine("\n\n...new title saved");
                Console.ReadLine();
            }

            else Console.WriteLine("User not found!");
        }

        catch (Exception e)
        {
            Console.WriteLine("Exception caught:\n\n" + e.ToString());
        }
    }

    static DirectoryEntry createDirectoryEntry()
    {
        // create and return new LDAP connection with desired settings  
        DirectoryEntry ldapConnection = new DirectoryEntry("abc.ca");
        ldapConnection.Path = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk";
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
        return ldapConnection;
    }

Solution

  • I think your issue is that some (read most) attributes don't get set by default on an object, they only exist at all against that object when a value is set for the first time. You need to update your code to handle the value not being present at all and assume that means it has no value set.

    You can test this by running the query against a specific user, observing the lack of the mobile property, then adding a value to that field and then removing it. You should then see that the property from then on is included in the list for that user.