Fork me on GitHub

Search Active Directory for User information [AX2012]

A simplified example from AD User Import Wizard on how to retrieve AD user details via X++.

It may be useful when the wizard doesn’t return all information that you need about system users.

Solution

public void SearchADUsers(Args _args)
{
    InteropPermission                                   interopPermission;
    System.DirectoryServices.DirectorySearcher          directorySearcher;
    System.DirectoryServices.DirectoryEntry             entry;
    System.DirectoryServices.SearchResultCollection     searchResultCollection;
    System.DirectoryServices.SearchScope                searchScope;
    System.DirectoryServices.SearchResult               searchResult;
    System.DirectoryServices.PropertyCollection         propertyCollection;
    System.DirectoryServices.PropertyValueCollection    propertyValueCollection;
    str                                                 searchCriteria;
    int                                                 searchCount, i, length;
    str                                                 phone, email;
    str                                                 _networkAlias;

    ;

    /// Search Active Directory for User details

    interopPermission = new InteropPermission(InteropKind::ClrInterop);
    interopPermission.assert();

    // AD user name(s)
    _networkAlias = "*jones";

    try
    {
        searchScope = System.DirectoryServices.SearchScope::Subtree;
        entry = new System.DirectoryServices.DirectoryEntry("LDAP://" + "adservername"); // Change to yours
        directorySearcher = new System.DirectoryServices.DirectorySearcher(entry);
        directorySearcher.set_PageSize(65535);
        directorySearcher.set_CacheResults(false);
        directorySearcher.set_SearchScope(searchScope);
        searchCriteria = strFmt("(samaccountname=%1)", _networkAlias);
        directorySearcher.set_Filter(strFmt('(&(objectClass=user)(objectCategory=person)%1(userAccountControl:1.2.840.113556.1.4.803:=512))', searchCriteria));
        searchResultCollection = directorySearcher.FindAll();
        searchCount = searchResultCollection.get_Count(); // In case more than 1 result is returned.

        for (i = 0; i < searchCount; i++)
        {
            searchResult = searchResultCollection.get_Item(i);
            entry = searchResult.GetDirectoryEntry();

            if (entry)
            {
                propertyCollection = entry.get_Properties();
            }

            if (!propertyCollection)
            {
                entry.Dispose();
                continue;
            }

            // User properties list
            try
            {
                // Phone
                propertyValueCollection = propertyCollection.get_Item('telephoneNumber');
                if (PropertyValueCollection)
                {
                    if (PropertyValueCollection.get_Value())
                    {
                        phone = propertyValueCollection.get_Value();
                    }
                }

                // Email
                propertyValueCollection = propertyCollection.get_Item('mail');
                if (PropertyValueCollection)
                {
                    if (PropertyValueCollection.get_Value())
                    {
                        email = propertyValueCollection.get_Value();
                    }
                }

                // Use returned results
                info(strFmt("Phone: %1, email: %2", phone, email));
            }
            catch (Exception::CLRError)
            {
                SRSProxy::handleClrException(Exception::Warning);
                warning(strFmt("@SYS117734"));
            }
            entry.Dispose();
        }
        searchResultCollection.Dispose();
    }
    catch (Exception::CLRError)
    {
        SRSProxy::handleClrException(Exception::Warning);
        error("@SYS117735");
    }

    CodeAccessPermission::revertAssert();
}

More information

  • MSDN, n.d., SysUserADUserImportWizard Class [online] Available at: https://msdn.microsoft.com/en-us/library/sysuseraduserimportwizard.aspx [Accessed 21 April 2015]

Comments !

links

social