Wednesday, November 2, 2011

Querying a user from active directory


public List<Users> GetUsers()
        {
            DirectoryEntry de = new DirectoryEntry(ConfigurationManager.AppSettings["ADSPath"]);
            DirectorySearcher Dsearch = new DirectorySearcher(de);
            Dsearch.Filter = "(&(objectClass=User))";
            List<Users> lstEmployees = new List<Users>();
            try
            {
                foreach (SearchResult sResultSet in Dsearch.FindAll())
                {
                    if (!string.IsNullOrEmpty(txtADUserName.Text.Trim()))
                    {
                        if (GetProperty(sResultSet, "cn").ToString() == txtADUserName.Text.Trim())
                        {
                            if (GetProperty(sResultSet, "mail").ToString() != "")
                            {
                             
                                    Users objUser = new Users();
                                    objUser.FirstName = GetProperty(sResultSet, "cn").ToString();
                                    objUser.EmailID = GetProperty(sResultSet, "mail").ToString();
                                    if (!string.IsNullOrEmpty(GetProperty(sResultSet, "manager").ToString()))
                                        objUser.ReportingManager = GetProperty(sResultSet, "manager").ToString().Split(',')[0].Substring(3).ToString();
                                    else
                                        objUser.ReportingManager = "";
                                    objUser.Designation = GetProperty(sResultSet, "title").ToString();
                                    objUser.EmployeeId = GetProperty(sResultSet, "employeeid").ToString();
                                    objUser.AdEntryDate = Convert.ToDateTime(GetProperty(sResultSet, "whencreated").ToString());
                                    objUser.Mobile = GetProperty(sResultSet, "mobile").ToString();

                                    lstEmployees.Add(objUser);                             
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(txtEmpId.Text.Trim()))
                    {
                        if (GetProperty(sResultSet, "mail").ToString() != "")
                        {
                            if (GetProperty(sResultSet, "employeeid").ToString().Equals(txtEmpId.Text.Trim()))
                            {
                                Users objUser = new Users();
                                objUser.FirstName = GetProperty(sResultSet, "cn").ToString();
                                objUser.EmailID = GetProperty(sResultSet, "mail").ToString();
                                if (!string.IsNullOrEmpty(GetProperty(sResultSet, "manager").ToString()))
                                    objUser.ReportingManager = GetProperty(sResultSet, "manager").ToString().Split(',')[0].Substring(3).ToString();
                                else
                                    objUser.ReportingManager = "";
                                objUser.Designation = GetProperty(sResultSet, "title").ToString();
                                objUser.EmployeeId = GetProperty(sResultSet, "employeeid").ToString();
                                objUser.AdEntryDate = Convert.ToDateTime(GetProperty(sResultSet, "whencreated").ToString());
                                objUser.Mobile = GetProperty(sResultSet, "mobile").ToString();

                                lstEmployees.Add(objUser);
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "alert_msg", "alert('"+ ex.Message.ToString() +"');", true);
            }
            return lstEmployees;

        }

        public static string GetProperty(SearchResult searchResult, string PropertyName)
        {
            if (searchResult.Properties.Contains(PropertyName))
            {
                return searchResult.Properties[PropertyName][0].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        public static bool GetPropertyValue(SearchResult searchResult, string PropertyName, string[] Values)
        {

            if (searchResult.Properties.Contains(PropertyName))
            {
                ResultPropertyValueCollection ht = searchResult.Properties[PropertyName];
                IEnumerator enumerator = ht.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    for (int i = 0; i < Values.Length; i++)
                    {
                        if (enumerator.Current.ToString().Contains(Values[i]))
                        {
                            return true;
                        }
                    }

                }
                return false;
            }
            else
            {
                return false;
            }
        }