Harish Raj
Harish Raj

Reputation: 1575

getting Manager's email ID from LDAP?

I am trying to get a person's Email ID as well as his/her manager's email ID. And the following is the code I tried.

DirContext ctx = new InitialDirContext(LDAPDetails());
String[] attrIDs = {"sAMAccountName", "cn", "title", "mailnickname", "mail", "manager", "department", "telephoneNumber"};
    SearchControls ctls = new SearchControls();
    ctls.setReturningAttributes(attrIDs);
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    String filter = "(CN=285263)";
    NamingEnumeration<SearchResult> answer = ctx.search("OU=users,DC=cts,DC=com", filter , ctls);
    answer = ctx.search("OU=xyz,DC=cts,DC=com", filter , ctls);

   while (answer.hasMore()) {
   SearchResult sr = (SearchResult) retEnum.next();
   Attribute mailAttribute=sr.getAttributes().get("mail");
   System.out.println("Team Member's eMail: "+mailAttribute.get()); //Here I am able to get the person's email.
   Attribute managerAttribute=sr.getAttributes().get("manager"); // this is just getting the manager's CN value. Not the email ID.
   }

Can someone help me get the manager's email ID please? Thanks in advance.

Upvotes: 0

Views: 6823

Answers (2)

Narasimha Vuppala
Narasimha Vuppala

Reputation: 11

Here is my approach...

1..Connecto to LDAP

2..Search the user and Get manager name

3.. Search the LDAP with the manager name and object class as user in filter condition.

NamingEnumeration answermanager = context.search(managerAttribute.get().toString(),"(objectClass=user)",searchCtls);

Upvotes: 1

Andreas Dolk
Andreas Dolk

Reputation: 114807

You'll have to lookup the manager(s) to get his (their) email address(es):

DirContext ctx = new InitialDirContext(LDAPDetails());
String[] attrIDs = { "sAMAccountName", "cn", "title", "mailnickname", "mail", "manager", "department", "telephoneNumber" };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(CN=285263)";
NamingEnumeration<SearchResult> answer = ctx.search("OU=users,DC=cts,DC=com", filter, ctls);

while (answer.hasMore()) {
    SearchResult sr = (SearchResult) retEnum.next();
    Attribute mailAttribute = sr.getAttributes().get("mail");
    System.out.println("Team Member's eMail: " + mailAttribute.get()); // Here I am able to get the person's email.
    Attribute managerAttribute = sr.getAttributes().get("manager"); // this is just getting the manager's CN value. Not the email ID.

    // now lookup the manger
    NamingEnumeration<SearchResult> managerAnswer = ctx.search(managerAttribute.get(), "", ctls);
    while (answer.hasMore()) {
        SearchResult managerSr = (SearchResult) retEnum.next();
        Attribute mailAttribute = sr.getAttributes().get("mail");
        System.out.println("Managers eMail: " + mailAttribute.get()); 
    }
}

Upvotes: 2

Related Questions