Solved

populate listbox with domain users

Posted on 2003-12-10
12
816 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:tariqf
  • 9
  • 3
12 Comments
 
LVL 19

Expert Comment

by:Rimvis
ID: 9911629
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
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 9911633
Sorry, forgot MoveNext

While Not rstUsers.EOF
   'do whatever you want
   rstUsers.MoveNext
Whend
0
 
LVL 1

Author Comment

by:tariqf
ID: 9911855
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Rimvis
ID: 9911880
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"
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 9911890
Also, you would want to change ordering.

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

Expert Comment

by:Rimvis
ID: 9911907
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/
0
 
LVL 1

Author Comment

by:tariqf
ID: 9912043
this may seem like a stupid question but how to I refer to each user? e.g. wscript.echo sAMAccountName
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 9912127
wscript.echo rstUsers("sAMAccountName").Value
0
 
LVL 19

Accepted Solution

by:
Rimvis earned 200 total points
ID: 9912140
And for ASP:

Response.Write rstUsers("sAMAccountName").Value & "<br>"
0
 
LVL 1

Author Comment

by:tariqf
ID: 9912261
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?
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 9912473
physicalDeliveryOfficeName?
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 9912494
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Programatically edit Web.config 8 40
Having Returning  Anonymous Function Access Global Function 7 113
AngularJS directive issue 6 80
Create animated movies for web page 18 124
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question