locked
Retrieve only name and telephonenumber RRS feed

  • Question

  • User225542415 posted
    I'm trying to retrieve only the name and telephonenumber from a specific OU, this is going to a new ListBox. It works fine if I only try to retrieve the name, but as soon as I add a new attribute, I get an exception:

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

     string name = sr.Properties["name"][0].ToString();
    Line 148: string nr = sr.Properties["telephonenumber"][0].ToString();
    Line 149:
    Line 150: ListBox2.Items.Add(name);
     
    This is the code I use:
     
    1    public void GetUsersFromOU(string ou)
    2        {
    3            
    4            string path = "LDAP://sys001.sys.no/OU=" + ou + ",DC=sys,dc=no";
    5            string[] attribs = new string[] { "name","telephonenumber" };
    6            string sFilter = String.Format("(&(objectClass=user)(objectCategory=person))");
    7            
    8    
    9            using (DirectoryEntry de = new DirectoryEntry(path))
    10           {
    11               de.AuthenticationType = AuthenticationTypes.Secure;
    12               
    13               DirectorySearcher ds = new DirectorySearcher(de, sFilter, attribs, SearchScope.OneLevel);
    14               
    15               int i = 0;
    16   
    17               SearchResultCollection src = ds.FindAll();
    18          
    19               foreach (SearchResult sr in src)
    20               {
    21                   string name = sr.Properties["name"][0].ToString();
    22                   string nr = sr.Properties["telephonenumber"][0].ToString(); //THROWS THE ERROR
    23   
    24                   ListBox2.Items.Add(name);
    25                   ListBox2.Items[i].Value = nr;
    26                   ListBox2.Items[i].Text = name;
    27                   i++;
    28                   name = null;
    29                   nr = null;
    30   
    31               }
    32           
    33               
    34           }
    
      What am I doing wrong here?
     

    Wednesday, September 20, 2006 5:01 AM

All replies

  • User225542415 posted

    I've managed to get the list now, but if a user does NOT have any value in mobile in AD, then it's not possible to select the user in the listbox, it just selects the first person AFTER a person that has a mobile number in AD.

    This is the code:

     

    1        public void GetUsersFromOU(string ou)
    2        {
    3            ListBox2.ClearSelection();
    4            txtSms.Text = "";
    5            string path = "LDAP://sys001.sys.no/OU=" + ou + ",DC=sys,dc=no";
    6            
    7    
    8             using (DirectoryEntry de = new DirectoryEntry(path))
    9                {
    10                   de.AuthenticationType = AuthenticationTypes.Secure;
    11                   string sFilter = String.Format("(&(objectCategory=Person)(objectClass=user))");
    12                   //these are the attributes that will show           
    13                   string[] attribs = new string[] {  "displayName", "mobile",};
    14   
    15                   DirectorySearcher ds = new DirectorySearcher(de, sFilter, attribs);
    16                   int i = 0;
    17                   using (SearchResultCollection src = ds.FindAll())
    18                   {                    
    19                       foreach (SearchResult sr in src)
    20                       {
    21                           StringBuilder name = new StringBuilder();
    22                           StringBuilder nr = new StringBuilder();
    23   
    24                           foreach (string key in attribs)
    25                           {
    26                               if (sr.Properties.Contains(key))
    27                               {
    28                                   foreach (object o in sr.Properties[key])
    29                                   {
    30                                       if (key.ToString() == "displayName")
    31                                       {
    32                                           name.AppendFormat("{0}", o);
    33                                       }
    34                                       if (key.ToString() == "mobile")
    35                                       {
    36                                           nr.AppendFormat("{0}", o);
    37                                       }                                    
    38                                   }                                
    39                               }                            
    40                           }
    41                           ListBox2.Items.Add(name.ToString());
    42                           ListBox2.Items[i].Value = nr.ToString();
    43                           ListBox2.Items[i].Text = name.ToString();
    44   
    45                           i++;
    46                           txtSms.Text += name.ToString() + "..." + nr.ToString() + "\r\n";
    47                           name = null;
    48                           nr = null;
    49                       }
    50                   }
    51            }
    52       }
    
      
    Wednesday, September 20, 2006 8:37 AM
  • User1297008538 posted

    You need to check that the value isn't null before you use it:

    if (!(sr.Properties["telephonenumber"][0] == null))
    {
         // Do something
    }
    
     
    Wednesday, September 20, 2006 5:12 PM
  • User1354132231 posted

    You need to check that the value isn't null before you use it:

    if (!(sr.Properties["telephonenumber"][0] == null))
    {
    // Do something
    }

     

    Unfortunately, that will blow up if the attribute does not exist on the object (since index 0 won't exist).  You need to use the .Contains() method.  This is described in the Common Patterns FAQ at the top of the forum.

     

    Wednesday, September 20, 2006 6:04 PM