Link to home
Start Free TrialLog in
Avatar of tariqf
tariqf

asked on

populate listbox with domain users

How do I populate a listbox (on a form) with all users from my domain? I have some vbscript as a starting point that will list the current user, but how do I get this to iterate through each user and populate my listbox? My plan is to have a seperate listbox for each location. Also I'm unsure if this script will work directly in ASP.

' use ADSI to get name office location
Const E_ADS_PROPERTY_NOT_FOUND = &H8000500D
Dim objDSE, objUser, varPropValue, arrPropValues, i
Dim strLoginHours, strByte
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objDSE = GetObject("LDAP://rootDSE")
Set objUser = GetObject("LDAP://CN=" & WshNetwork.UserName & ",CN=Users" & "," & objDSE.Get("defaultNamingContext"))
thename = objUser.Get("sAMAccountName")
location = objUser.OfficeLocations
Avatar of Rimvis
Rimvis
Flag of Lithuania image

I woult suggest using ADO driver for Active Directory instead

Here's sample code:


Dim rstUsers
Dim objDSE, strNamingContext

Set objDSE = GetObject("LDAP://rootDSE")
strNamingContext = objDSE.Get("defaultNamingContext")
Set objDSE = Nothing

set rstUsers= Server.CreateObject("ADODB.Recordset")
rstUsers.Open , "SELECT sAMAccountName FROM 'LDAP://CN=Users," & strNamingContext  & "' WHERE objectClass='user' ORDER BY name", "Provider=ADsDSOObject"

While Not rstUsers.EOF
   'do whatever you want
Whend

rstUsers.Close
Set rstUsers = Nothing


rstUsers.Close
Sorry, forgot MoveNext

While Not rstUsers.EOF
   'do whatever you want
   rstUsers.MoveNext
Whend
Avatar of tariqf
tariqf

ASKER

Hi, I have tried your response, but I get a "type missmatch" on this line;

rstUsers.Open , "SELECT sAMAccountName FROM 'LDAP://CN=Users," & strNamingContext  & "' WHERE objectClass='user' ORDER BY name", "Provider=ADsDSOObject"

any ideas? PLease note I am running this with wsh before I migrate it to ASP so I removed the server in server.createobject
Oops, typo. Remove comma after rstUsers.Open
Should be

rstUsers.Open "SELECT sAMAccountName FROM 'LDAP://CN=Users," & strNamingContext  & "' WHERE objectClass='user' ORDER BY name", "Provider=ADsDSOObject"
Also, you would want to change ordering.

rstUsers.Open "SELECT sAMAccountName FROM 'LDAP://CN=Users," & strNamingContext  & "' WHERE objectClass='user' ORDER BY sAMAccountName", "Provider=ADsDSOObject"
Hm, and another error. Remove last "rstUsers.Close".

My apologies for this mess, I remade this sample from my project, so there were a few errors during copy-paste-editing :o/
Avatar of tariqf

ASKER

this may seem like a stupid question but how to I refer to each user? e.g. wscript.echo sAMAccountName
wscript.echo rstUsers("sAMAccountName").Value
ASKER CERTIFIED SOLUTION
Avatar of Rimvis
Rimvis
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tariqf

ASKER

Hi, thanks for your help, I cannot seem to get the officelocation as I used to though - I have tried using

SELECT sAMAccountName,OfficeLocations FROM 'LDAP://CN=Users...

but this doesn't work. Can you help please?
physicalDeliveryOfficeName?
Check this link for complete list of Organizational-Person class properties:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adschema/ad/win2k_c_organizationalperson.asp