Solved

populate listbox with domain users

Posted on 2003-12-10
12
812 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now