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("WScr ipt.Networ k")
Set objDSE = GetObject("LDAP://rootDSE" )
Set objUser = GetObject("LDAP://CN=" & WshNetwork.UserName & ",CN=Users" & "," & objDSE.Get("defaultNamingC ontext"))
thename = objUser.Get("sAMAccountNam e")
location = objUser.OfficeLocations
' 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("WScr
Set objDSE = GetObject("LDAP://rootDSE"
Set objUser = GetObject("LDAP://CN=" & WshNetwork.UserName & ",CN=Users" & "," & objDSE.Get("defaultNamingC
thename = objUser.Get("sAMAccountNam
location = objUser.OfficeLocations
Sorry, forgot MoveNext
While Not rstUsers.EOF
'do whatever you want
rstUsers.MoveNext
Whend
While Not rstUsers.EOF
'do whatever you want
rstUsers.MoveNext
Whend
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
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"
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"
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/
My apologies for this mess, I remade this sample from my project, so there were a few errors during copy-paste-editing :o/
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi, thanks for your help, I cannot seem to get the officelocation as I used to though - I have tried using
SELECT sAMAccountName,OfficeLocat ions FROM 'LDAP://CN=Users...
but this doesn't work. Can you help please?
SELECT sAMAccountName,OfficeLocat
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
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adschema/ad/win2k_c_organizationalperson.asp
Here's sample code:
Dim rstUsers
Dim objDSE, strNamingContext
Set objDSE = GetObject("LDAP://rootDSE"
strNamingContext = objDSE.Get("defaultNamingC
Set objDSE = Nothing
set rstUsers= Server.CreateObject("ADODB
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