Apr 20, 2016

AD user lookup in AX 2012

static void adGroups(Args _args)
{
    System.DirectoryServices.AccountManagement.PrincipalContext yourDomain;
    System.DirectoryServices.AccountManagement.UserPrincipal user;
    System.DirectoryServices.AccountManagement.GroupPrincipal p;
    CLRObject groups, enum;
    System.String domain, username1,groupName;
    str groupN;
    Userid userId = curUserId();
    InteropPermission permission;

    UserInfo userInfo;
    try
    {
        permission = new InteropPermission(InteropKind::CLRInterop);
        permission.assert();

        yourDomain = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType::Domain);

        // find your user
        userInfo = xUserInfo::find(false, userId);
        domain = UserInfo.networkDomain;
        username1 = UserInfo.networkAlias;
        user = System.DirectoryServices.AccountManagement.UserPrincipal::FindByIdentity(yourDomain, username1);

        // if found - grab its groups

        if(user != null)
        {
            groups = user.GetAuthorizationGroups();
            enum = groups.GetEnumerator();

            while (enum.MoveNext())
            {
                p = enum.get_Current();
                groupName = p.get_Name();
                groupN = groupName;
                info(groupN);
            }

        }
        CodeAccessPermission::revertAssert();
     }
     catch (Exception::CLRError)
     {
         CodeAccessPermission::revertAssert();
         info(CLRInterop::getLastException().ToString());
     }
}

No comments:

Post a Comment