Reading LDAP in a script

jpro
Mega Contributor

I have a need to read an LDAP attribute (one that is not captured as part of our LDAP integration with sys_user) within a script. Is this possible and are there examples. Thanks.

3 REPLIES 3

adiddigi
Tera Guru

Here is the code, that you can use to get the user information from LDAP after passing the user id. Take the idea from here.


Also, If you are pre-calgary, Package calls will work. If you are on Calgary check this link and replace package calls:

http://wiki.servicenow.com/index.php?title=Packages_Call_Replacement_Script_Objects



var LDAPUtilCC = Class.create();

LDAPUtilCC.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getUserGUID : function(userName){
var strUserId = new String(userName);
strUserId = strUserId.replaceAll(/[^a-zA-Z 0-9*]+/g,'');
return this._queryLDAP(strUserId);

},

_queryLDAP : function(strUserId){
var userGuid = "";
try{
var rec = new GlideRecord('ldap_server_config');
rec.get('fda9e90cac162c5e019dfeb9ffd9a3df');
var ldapc = new Packages.com.glide.sys.ldap.LDAPConfig(rec);
var ldap = new Packages.com.glide.sys.ldap.LDAP();
ldap.setConfig(ldapc);
var query = '';

if(!JSUtil.nil(strUserId)){
query = "(sAMAccountName=" + strUserId +")";
}
var result = ldap.getMatching('',query,true,1000);

if(!JSUtil.nil(result)){
while(test = result.next()){
var strResult = test.toString();
gs.log(strResult);
}
}
}catch{
gs.log("LDAPUtilCC: " + e);
}
return userGuid;
},
type : "LDAPUtilCC"

});



9 years later, is there an updated version of this for Rome? I ran the package remover but the replacement script does not work.

jpro
Mega Contributor

Fantastic, thank you this will get me going!