Tuesday, July 14, 2015

Read User Properties From Active Directory

Read User Properties From Active Directory:-
lblMessage.Text = GetEmployeeID("name@company.com"); 
internal static string GetEmployeeID (string strmail)
        {
            StringBuilder sbQuery = new StringBuilder();
            string stremployeeid = string.Empty;           
            string strLdapPath = "LDAP://DC=company_domain,DC=com";
            try
            {
                if (string.IsNullOrEmpty(Convert.ToString(sbQuery)))
                {
                    sbQuery.Append("(&(objectClass=user)(|(mail=" + strmail + ")");
                }
                sbQuery.Append("))");
                DirectoryEntry entry = new DirectoryEntry(strLdapPath);
                DirectorySearcher userSearch = new DirectorySearcher(entry);
                userSearch.Filter = Convert.ToString(sbQuery);
                userSearch.PropertiesToLoad.Add("mail");
                userSearch.PropertiesToLoad.Add("employeeid");
                SearchResultCollection sresultcoll = userSearch.FindAll();
                foreach (SearchResult sresult in sresultcoll)
                {
                    if (sresult != null)
                    {
                        if (sresult.Properties["mail"].Count == 1)
                        {
                            stremployeeid = sresult.Properties["employeeid"].Count > 0 ? Convert.ToString(sresult.Properties["employeeid"][0]) : string.Empty;
                            if (stremployeeid != null && stremployeeid.Length > 0)
                            {
                                return stremployeeid;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return stremployeeid;
        }

No comments:

Post a Comment

Featured Post

Create SharePoint Folder Structure in Destination (Only If Not Exists)

Why This Script Is Safe You can run it multiple times It will not create duplicate folders It will only create missing folders S...

Popular posts