How to retrive user's details from Active Directory (AD) in AX 2012 using X++


Below is a small piece of code which may going to help you to get user's information from Active Directory (AD).
In my case I am receiving only email id details of a user, Using First and last name.
You may use for other fields like department, title​, displayName, mail, member, distinguishedName​ and others if any.​

Public str 60 GetDetailsFromAD(str 100  _FirstName="",str 100 _LastName="")
{
    System.DirectoryServices.DirectorySearcher DirectorySearcher;
    System.DirectoryServices.SearchScope SearchScope;
    System.DirectoryServices.DirectoryEntry DirectoryEntry;

    System.DirectoryServices.SearchResultCollection SearchResultCollection;
    System.DirectoryServices.SearchResult SearchResult;

    System.DirectoryServices.PropertyCollection PropertyCollection;
    System.DirectoryServices.PropertyValueCollection PropertyValueCollection;

    str networkDomain=#DomainName;
    str prefix = 'LDAP://';

    int totalCount;
    int counter;

    str mysamaccountname,criteria,_ADUserEmail;
    str myusername;
    ;

    try
    {
        if(_FirstName && _LastName)
        {
            DirectoryEntry = new System.DirectoryServices.DirectoryEntry(prefix + networkDomain);
            SearchScope = CLRInterop::parseClrEnum('System.DirectoryServices.SearchScope', 'Subtree');

            DirectorySearcher = new System.DirectoryServices.DirectorySearcher(DirectoryEntry);
            DirectorySearcher.set_SearchScope(searchScope);
            //DirectorySearcher.set_Filter(strfmt('(&(objectClass=user))'));
            //SearchResultCollection = DirectorySearcher.FindAll();

            criteria += strFmt('(givenName=%1)', _FirstName) ;
            criteria += strFmt('(sn=%1)', _LastName) ;

            directorySearcher.set_Filter(strFmt('(&(objectClass=user)(objectCategory=person)%1(userAccountControl:1.2.840.113556.1.4.803:=512))', criteria));
            searchResultCollection = directorySearcher.FindAll();
            totalCount = SearchResultCollection.get_Count();
            for (counter=0; counter < totalcount; counter++)
            {
                SearchResult = SearchResultCollection.get_Item(counter);
                DirectoryEntry = SearchResult.GetDirectoryEntry();

                if (DirectoryEntry)
                {
                    PropertyCollection = DirectoryEntry.get_Properties();
                    if (PropertyCollection)
                    {
                        PropertyValueCollection = PropertyCollection.get_Item('mail');
                        if (PropertyValueCollection && PropertyValueCollection.get_Count())
                        {
                            if (PropertyValueCollection.get_Value())
                            {
                                _ADUserEmail= PropertyValueCollection.get_Value();
                                if(_ADUserEmail!="")
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            DirectorySearcher.Dispose();
            SearchResultCollection.Dispose();
        }
    }
    catch (Exception::CLRError)
    {
        error(strFmt("@SYS117734"));
        return _ADUserEmail;
    }
    return _ADUserEmail;
}

Previous
Next Post »