开发者

Using JavaScript to get an LDAP multi-valued string attribute

I am trying to retrieve an object attribute in Active Directory that appears to be a multi-valued string (See canonicalName). After performing a search:

var conn;
conn.Open = 'Provider=ADsDSOObject;';
var command = '<LDAP://CN=Partitions,CN=Configuration,DC=domain,DC=com>;(&(objectcategory=crossRef)(systemFlags=3));name,nETBIOSName,nCName,canonicalName;onelevel';
var ado = new ActiveXObject('ADOD开发者_高级运维B.Command');
ado.ActiveConnection = conn;
ado.CommandText = command;
var records = ado.Execute;

and while looping through the recordset:

while (!records.Eof){
  ...

  records.MoveNext();
}

I then try and get the canonicalName:

var cn = records.Fields('canonicalName').Value;

The problem is that JavaScript doesn't intrepret cn as a string or as an array... if you look at the AD schema for the canonicalName you can see it's configured with 'isSingleValue = false' which is what I believe is causing the problem...

When stepping through the code with Visual Studio I can drill into the string value and the QuickWatch shows cn.value(0) as returning the string value. But when I type that out it doesn't work...

I have tried the usual ways to get the value without luck:

for (var i in cn) { alert(cn[i]); }

and

for (i = 0; i < cn.length; i++) { alert(cn[i]); }

Neither works...

How can I read the value of this object?


I know this is an older question but I figured it out and would like to share.

var ldap = GetObject("LDAP://cn=Group Name, ou=Name, dc=Domain");

var ldapArr = ldap.member.toArray();

for(var x = 0; x < ldapArr.length; x++) {
    WScript.Echo(ldapArr [x]);
    }

Hope that helps to you and anyone else banging their head over it like I have been.


Sometimes I've got the same problem: I can't iterate an object with javascript and however in vb it's done with a simple for each ...

I found a way to iterate in javascript that solved my problem. I hope it is usefull to you:

var enumCn = new Enumerator(cn);
for (; !enumCn.atEnd(); enumCn.moveNext()) {
    var cnItem = enumCn.item();

... do whatever you need with cnItem...

    };
};


I was using ADO Recordset for this, and thought I'd help future users passing by with this issue. It utilises the solution by Xiazer.

I'm using Internet Explorer 11 and a localhost Apache DS Explorer LDAP Server. ANd JQuery of course.

Just put this in any HTML file and open in Internet Explorer.

$(document).ready(function(){
    console.log("Starting");
    showUsers();
    console.log("Done")
});

function GetFieldValue(objField){
    var result = "";
    if (objField.Value == null){
    }
    else{
        if (objField.type == 12){
          var ldapArr = objField.Value.toArray();
          result = ldapArr[0];
        }
        else{
          result = objField.Value;
        }
    }
    return result;
}//GetFieldValue

function showUsers(){
  console.log("showUsers");
  var strAttributes, strFilter, SrchCriteria, strQuery;

  strAttributes = "cn,sn,entryDN"
  SrchCriteria="(objectClass=iNetOrgPerson)"

  var ADOCommand = new ActiveXObject("ADODB.Command");
  var ADOConnection = new ActiveXObject("ADODB.Connection");
  var ADORecordSet = new ActiveXObject("ADODB.RecordSet");
  ADOConnection.Provider = "ADsDSOObject";
  ADOConnection.Properties("User ID") = "uid=admin,ou=system"
  ADOConnection.Properties("Password") = "secret"
  strDomainName = "localhost:10389/dc=mijnldap,dc=local"
  strBase = "<LDAP://" + strDomainName + ">";

  ADOConnection.Open("Active Directory Provider");
  ADOCommand.ActiveConnection = ADOConnection;
  strFilter = SrchCriteria;
  strQuery = strBase + ";" + strFilter + ";" + strAttributes + ";subtree";
  strQuery = strQuery;

  console.log(strQuery);

  ADOCommand.CommandText = strQuery;
  ADORecordSet = ADOCommand.Execute;
  console.log("Query executed");

  if (ADORecordSet.RecordCount > 0) {
    ADORecordSet.MoveFirst;
    while (! ADORecordSet.EOF){
      console.log (GetFieldValue(ADORecordSet.Fields("sn")));
      console.log (GetFieldValue(ADORecordSet.Fields("cn")));
      console.log (GetFieldValue(ADORecordSet.Fields("entryDN")));
      ADORecordSet.MoveNext;
    }
  }//if 
  ADORecordSet.close

} //showUsers
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜