Active Directory description field values not showing up

I'm working on getting values from Active Directory from a script task in C# (3.5 framework) in SSIS. The code to accomplish this seems to be working fine. I use a nested loop with the outer loop pulling values that aren't multi-valued and inserting them to a table with a stored procedure, then passing back the PK of that row to be used as a FK in the second loop to a second stored procedure that inserts all the groups that user belongs to in a second table.

My problem is I can't get any values from what I believe should be the Description field that should hold the description of each individual group. If I step through it and check the count of description object, it shows 0. Using the same code to check the count of Memberof I get the number of groups that user belongs to. The other part that I don't understand is I get values from the Description field in the outer loop, but its closer to that of comments of the user record, not descriptions of the individual groups. Its as if my Description is not multivalued. But someone else can confirm through the AD portal that the Description field has values that describe what each group is. I'm kind of stumped. See code below.

DirectoryEntry entry = new DirectoryEntry("LDAP:[my address]");
DirectorySearcher Dsearch = new DirectorySearcher(entry);
Dsearch.Filter = "(&(objectClass=User))"; 

foreach (SearchResult sResultSet in Dsearch.FindAll())
{
    (code continues from original post)

    using (SqlConnection dataConnection = new SqlConnection([mysqlconnection]))
    {
        using (SqlCommand dataCommand = dataConnection.CreateCommand())
        {
           dataCommand.CommandText = "ActiveDirectory.InsertParentRecords";
           dataCommand.CommandType = CommandType.StoredProcedure;

           dataCommand.Parameters.AddWithValue("@PackageLogId", Dts.Variables["PackageLogId"].Value.ToString());
           dataCommand.Parameters.AddWithValue("@cn", GetProperty(sResultSet, "cn"));
           dataCommand.Parameters.AddWithValue("@givenName", GetProperty(sResultSet, "givenName"));
           dataCommand.Parameters.AddWithValue("@initials", GetProperty(sResultSet, "initials"));
           dataCommand.Parameters.AddWithValue("@sn", GetProperty(sResultSet, "sn"));

           dataCommand.Parameters.AddWithValue("@mail", GetProperty(sResultSet, "mail"));
           dataCommand.Parameters.AddWithValue("@Name", GetProperty(sResultSet, "Name"));
           dataCommand.Parameters.AddWithValue("@middleName", GetProperty(sResultSet, "middleName"));
           dataCommand.Parameters.AddWithValue("@title", GetProperty(sResultSet, "title"));
           dataCommand.Parameters.AddWithValue("@employeeID", GetProperty(sResultSet, "employeeID"));

           dataCommand.Parameters.AddWithValue("@employeeNumber", GetProperty(sResultSet, "employeeNumber"));
           dataCommand.Parameters.AddWithValue("@objectSid", ConvertSidToString((byte[])sResultSet.Properties["objectSid"][0]));
           dataCommand.Parameters.AddWithValue("@userAccountControl", tempuserAccountControl);
           dataCommand.Parameters.AddWithValue("@whenCreated", GetProperty(sResultSet, "whenCreated"));
           dataCommand.Parameters.AddWithValue("@distinguishedName", GetProperty(sResultSet, "distinguishedName"));

           dataCommand.Parameters.AddWithValue("@badPasswordTime", Convert.ToString(badPasswordTime));  //Issues!!
           dataCommand.Parameters.AddWithValue("@badPwdCount", GetProperty(sResultSet, "badPwdCount"));
           dataCommand.Parameters.AddWithValue("@memberof", GetProperty(sResultSet, "memberof"));
           dataCommand.Parameters.AddWithValue("@samaccountname", GetProperty(sResultSet, "samaccountname"));
           dataCommand.Parameters.AddWithValue("@Description", GetProperty(sResultSet, "Description"));

           dataCommand.Parameters.AddWithValue("@maxPwdAge", GetProperty(sResultSet, "maxPwdAge"));   //Issues!!                               
           dataCommand.Parameters.AddWithValue("@pwdLastSet", pwdLastSet);   //Issues!!
           dataCommand.Parameters.AddWithValue("@LockOutTime", Convert.ToString(LockOutTime));     //Issues!!

           if (disabled == false)  //Issues!!
              dataCommand.Parameters.AddWithValue("@Acctdisabled", '0');
           else
              dataCommand.Parameters.AddWithValue("@Acctdisabled", '1');

           dataCommand.Parameters.AddWithValue("@displayname", GetProperty(sResultSet, "displayname"));

           dataCommand.Parameters.AddWithValue("@twofactor", twofactor);
           dataCommand.Parameters.AddWithValue("@skiprecord", skiprecord);

           dataCommand.Parameters.Add("@DetailID", SqlDbType.Int);
           dataCommand.Parameters["@DetailID"].Direction = ParameterDirection.Output;

           dataConnection.Open();
           dataCommand.ExecuteScalar();
           dataConnection.Close();

           Counter++;
           DetailID = (int)dataCommand.Parameters["@DetailID"].Value;
       }
    }

    using (SqlConnection dataConnection = new SqlConnection[mysqlconnection]))
    {
       using (SqlCommand dataCommand = dataConnection.CreateCommand())
       {
           dataConnection.Open();

           int groupCount = sResultSet.Properties["memberOf"].Count;
           int DescriptionCount = sResultSet.Properties["Description"].Count;

           for (int counter = 0; counter < groupCount; counter++)
           {
               dataCommand.CommandText = "ActiveDirectory.InsertMemberOf";
               dataCommand.CommandType = CommandType.StoredProcedure;

               dataCommand.Parameters.Clear();
               dataCommand.Parameters.AddWithValue("@PackageLogId", Dts.Variables["PackageLogId"].Value.ToString());
               dataCommand.Parameters.AddWithValue("@DetailID", DetailID);

               if (sResultSet.Properties.Contains("Description"))
               {
                  dataCommand.Parameters.AddWithValue("@Group", sResultSet.Properties["Description"][counter].ToString());
               }
               else
               {
                  dataCommand.Parameters.AddWithValue("@Group", "n/a");
               }                               

               dataCommand.Parameters.AddWithValue("@memberOf", sResultSet.Properties["memberOf"][counter]);   

               dataCommand.ExecuteScalar();
               InnerCounter++;
            }
         }  //End of DataCommand
     }  //End of Data Connection 


     Debug.WriteLine(GetProperty(sResultSet, "displayname") + "  " + Counter + ",  " + InnerCounter + ",  " + GetProperty(sResultSet, "userAccountControl"));
     InnerCounter = 0;                        
  } //End of For Each Loop

Answers


A LDAP Search query is a bit like a SQL query, I know It's not clearly documented but you'd better declare the properties you want to be retrived by the search. Can you try to add

DirectorySearcher Dsearch = new DirectorySearcher(entry);
...
Dsearch.PropertiesToLoad.Add("description");
...
Dsearch.Filter = "(&(objectClass=User))"; 

In my code I really add all the properties I need. I agree it works without that most of the time, but just most of the time.


Need Your Help

How to improve execution speed on a select operation

mysql sql

create table foo (id int(11) auto_increment, value varchar(255))