VMware Cloud Community
scotlandrocks
Contributor
Contributor
Jump to solution

How to retrieve AD username

Hi all,

I'm sure what I'm trying to do is perfectly simple but I'm completely stuck as to how.

I've already retrieved the user currently running the workflow using "workflow.RunningUserName". What I'd then like to do is convert this to or get the "AD:user" attribute so that I can then get the users email address from Active Directory.

Is there any easy way to get the current running user and put it to an "AD:User" type attribute to work with.

Anyone who can advise? much appreciated.

0 Kudos
1 Solution

Accepted Solutions
ectoplasm88
Contributor
Contributor
Jump to solution

Server.getCurrentLdapUser() will probably serve you just fine.  If you need to convert it you can use ActiveDirectory.search() .  I also wrote this little function to search on a string since you may get back more than one match:

var ldapUsers = new Array();
ldapUsers = Server.searchLdapUsers(userString);

if (ldapUsers.length == 0) {
    System.log("No user named: " + userString + " found.");
}
else {

    System.log("Found " + ldapUsers.length + " matches for user: " + userString);
    System.log("");
   
    for each (var user in ldapUsers) {
               
        System.log("commonName: " + user.commonName);
        System.log("displayName: " + user.displayName);
        System.log("dn: " + user.dn);
        System.log("loginName: " + user.loginName);
        System.log("userPrincipalName: " + user.userPrincipalName);
        System.log("emailAddress: " + user.emailAddress);
       
    }
   
    for each (var user in ldapUsers) {
       
        if (user.commonName.toLowerCase() == userString.toLowerCase()) {
            System.log("Exact Match found!");
            System.log("commonName: " + user.commonName);
            System.log("displayName: " + user.displayName);
            System.log("dn: " + user.dn);
            System.log("loginName: " + user.loginName);
            System.log("userPrincipalName: " + user.userPrincipalName);
            System.log("emailAddress: " + user.emailAddress);
            return user;
        }
    }
   
}

View solution in original post

0 Kudos
2 Replies
ectoplasm88
Contributor
Contributor
Jump to solution

Server.getCurrentLdapUser() will probably serve you just fine.  If you need to convert it you can use ActiveDirectory.search() .  I also wrote this little function to search on a string since you may get back more than one match:

var ldapUsers = new Array();
ldapUsers = Server.searchLdapUsers(userString);

if (ldapUsers.length == 0) {
    System.log("No user named: " + userString + " found.");
}
else {

    System.log("Found " + ldapUsers.length + " matches for user: " + userString);
    System.log("");
   
    for each (var user in ldapUsers) {
               
        System.log("commonName: " + user.commonName);
        System.log("displayName: " + user.displayName);
        System.log("dn: " + user.dn);
        System.log("loginName: " + user.loginName);
        System.log("userPrincipalName: " + user.userPrincipalName);
        System.log("emailAddress: " + user.emailAddress);
       
    }
   
    for each (var user in ldapUsers) {
       
        if (user.commonName.toLowerCase() == userString.toLowerCase()) {
            System.log("Exact Match found!");
            System.log("commonName: " + user.commonName);
            System.log("displayName: " + user.displayName);
            System.log("dn: " + user.dn);
            System.log("loginName: " + user.loginName);
            System.log("userPrincipalName: " + user.userPrincipalName);
            System.log("emailAddress: " + user.emailAddress);
            return user;
        }
    }
   
}

0 Kudos
scotlandrocks
Contributor
Contributor
Jump to solution

Perfect thanks, really helpful!

Just incase it's useful to anyone else out there:

adUsername = Server.getCurrentLdapUser().commonName;

or

adEmail = Server.getCurrentLdapUser().emailAddress;

where adUsername and adEmail are of type string

So is the only way to use the AD:User to either specify manually or use the ActiveDirectory.search() ?

0 Kudos