, , , , ,

Hello Everyone,


This week I got a task to perform a LDAP query and fetch few user information. Challenge was to perform the fetch action not from our current domain but rather a different domain; that too not with FQDN but with IP. This was not too tricky as all I need to do was configure the DirectoryEntity first and then pass it to the usual DirectorySearcher which we use to perform LDAP.


Let me explain that in detail, step by step:


1> I started with creating a new Console App in Visual Studio.


2>  To begin with, I needed to add the System.DirectoryServices (for LDAP) and System.Configurations (for configuration file) as assembly references. 


3> Initialize the DirectoryEntity object with the following:

    • Authenticating accountId
    • Authenticating Password
    • LDAP path (can either be Server IP containing AD <“LDAP://”> or the FQDN of AD <“LDAP://DC=mscrm,DC=com,DC=sg”>)


4>  Initialize the DirectorySearcher object with the DirectoryEntry.

This would do a DNS lookup to find a DC where the AD resides.

I used the DirectorySearcher.SearchRoot.Path to check if the path is set correctly for LDAP.


5> Now it’s time to set the filter properties of the DirectorySearcher with the LDAP query.

I’ll pass on the LDAP query to Configuration Manager AppSettings as the query will change from time-to-time.


6> Once everything is set, it’s time for the results. Results are captured using SearchResult with:

    • FindOne() – when fetching a specific user details with 1 results only. Something like in SDK we have IOrganizationService.Retrieve Method
    • FindAll() – when fetching multiple users with a filter criteria. Something like in SDK we have IOrganizationService.RetrieveMultiple Method 



7> Build the code and .exe is ready to be executed in the server. 🙂

The App.config file will be like this


8> And the final Result of query, if everything falls in place correctly.


Hope this was helpful. Thank much!  🙂