Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi ,

This is my code below,am not sure is this anyway around to connect with Active Directory and pull the data efficiently.

when ever it hits this method it shows up exchange server is running 100% proc memory.

Some one advise how to do this efficiently,thanks in Advance
public void FixUserPhoneNumberfromAD(EmployeeExport item, SqlConnection sqlConnection1)
        {
            string phonenumber = string.Empty; // we will get this value from the AD
            string EmailID = string.Empty;
            string employeeNumber = string.Empty; // we will get this from AD. 
            string UserId = string.Empty; // we will get a user Id from AD.

            try
            {
                
                DirectoryEntry entry = new DirectoryEntry("DomainName");
                DirectorySearcher searcher = new DirectorySearcher();
                searcher.Filter = "(otherpager=" + item.EmployeeNumber + ")";
                foreach (SearchResult result in searcher.FindAll())
                {
                    phonenumber = result.GetDirectoryEntry().Properties["telephonenumber"].Value.ToString();
                    EmailID =     result.GetDirectoryEntry().Properties["mail"].Value.ToString();
                                     
                    entry.Close();
                    if (!String.IsNullOrEmpty(phonenumber))
                    {
                        ///go to the database and update the phone number
                        /// 
                        UpdateDatabasewithPhoneNumber(item.EmployeeNumber, phonenumber, sqlConnection1);
                        UpdateDatabasewithEmail(item.EmployeeNumber, EmailID, sqlConnection1);
                        
                       // entry.Close();
                       
                    }
                    break;
                } 
            }
            catch (System.Exception se) // exception on the console. 
            {
                Console.WriteLine(se.Message);
            }
        }
Posted
Updated 14-Mar-11 20:22pm
v4
Comments
Sergey Alexandrovich Kryukov 15-Mar-11 2:13am    
Good Active Directory -- dead Active Directory.
--SA
shan1395 15-Mar-11 2:16am    
i don't know SA...but my code kills Exchange Server

1. Make your search criteria more restrictive. For example, since it looks like you are searching only for people, you could set objectCategory=person, etc.
2. Request only the properties that you use (telephonenumber and mail) by calling searcher.PropertiesToLoad.Add.
3. Set the page size to something other than zero (say, 1000).
4. To avoid a memory leak, don't forget to call Dispose() on the results returned from the call to FindAll().
 
Share this answer
 
Comments
Manfred Rudolf Bihy 15-Mar-11 8:43am    
[Moved from OP'S solution]
Thanks dasblinkenlight,
I will follow your advice in my code.i will let you know how things goin on.
Thanks Again
shan1395 15-Mar-11 18:20pm    
Thanks dasblinkenlight and Dylan Morley,

Based on your both amazing examples and advice modified my code like below,but still it takes approx 4sec to pull the record.

Please advice me

DirectoryEntry entry = new DirectoryEntry(DomainName);
DirectorySearcher searcher = new DirectorySearcher();
searcher.Filter = ("(&(objectClass=user)(otherpager=" + item.EmployeeNumber + "))";
searcher.PropertiesToLoad.Add("telephonenumber");
searcher.PropertiesToLoad.Add("mail");
foreach (SearchResult result in searcher.FindAll())
{

phonenumber = result.GetDirectoryEntry().Properties["telephonenumber"].Value.ToString();
EmailID = result.GetDirectoryEntry().Properties["mail"].Value.ToString();
entry.Dispose();
}
dasblinkenlight 15-Mar-11 22:41pm    
From my limited and dated experience with the directory services, I remember that searching by non-structural attributes is usually a lot slower than by "structural" ones. Is there a way to restrict your entities by something "stronger" than your otherpager attribute? Unlike RDBMS, you can't tell your directory server to index a particular attribute, so in a sense, you have to "jump through the hoops" to make your queries perform. Faced with a situation when we were allowed to run our queries against the directory server only at specific times to avoid hampering the server performance (our queries were rather demanding), we ended up building a system that copied LDAP entities of interest to an RDBMS repository, and queried that repository instead of LDAP.
shan1395 16-Mar-11 1:54am    
Thanks a lot for all your help dablinkenlight.

I don't want to deal directly with AD,so i have alternate plan.am going to grab all the active users in AD and store in DB.

then i will query my DB to get the value i want,is that make sense ?

Thanks
dasblinkenlight 16-Mar-11 6:43am    
Yes, that's what we did. We also established a process to sync up the DB from the AD periodically. There is a set of APIs for AD mirroring, letting you code a reliable synchronization relatively easily. Unfortunately, I wasn't working on that part of the system (and the system was coded in Java, too), so I don't remember which API it was.
Follow dasblinkenlight's advice, I use something like this to search for users where their common name wildcard matches a string. It follows all of the advice given and should demonstrate the type of thing you need to do

C#
private List<ActiveDirectoryUser> GetDomainUsers(string name)
{
    var userList = new List<ActiveDirectoryUser>();

    using (var root = new DirectoryEntry(domainName))
    {
        using (var s = new DirectorySearcher(root))
	{
	    s.Filter = String.Format("(&(objectCategory=Person)(cn=*{0}*))", name);

	    s.PropertiesToLoad.Add("cn"); // full name
	    s.PropertiesToLoad.Add("sAMAccountName"); // domain account name
            s.PropertiesToLoad.Add("givenName"); // firstname
	    s.PropertiesToLoad.Add("sn"); // surname

	    using (var results = s.FindAll())
	    {
		for (int i = 0; i < results.Count; i++)
		{
		    SearchResult result = results[i];
		    var user = new ActiveDirectoryUser(result);
			
		    userList.Add(user);
		}
	    }
	}
    }        
    return userList;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900