• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

ASP & LDAP Info on Users

Hi,
A simple one for an expert, I'm sure. I'm tring to pull out info from my 2003 AD in to a web page via ASP to show basic contact info on users from specific groups. my ASP looks like this:

<%
strUser = "Domain\User"
strPassword = "Password"
SDomain = "Domain"
sLogonDomain = "User or Search"
Dim Conn2, strRS2, RS2, strConn2
Set Conn2 = Server.CreateObject("ADODB.Connection")
Set RS2 = Server.CreateObject("ADODB.Recordset")
Conn2.Provider = "ADsDSOObject"
Conn2.Properties("User ID") = strUser
Conn2.Properties("Password") = strPassword
Conn2.Properties("Encrypt Password") = True
strConn2 = "Active Directory Provider"
Conn2.Open strConn2 , strUser, strPassword
strRS2 = "SELECT givenname,sn,title,telephoneNumber,mail FROM 'LDAP://" & SDomain &"' WHERE sAMAccountName = '"&sLogonDomain &"' ORDER by name "

RS2.Open strRS2, Conn2,1,1
While RS2.EOF = False      
          response.write  rs2("givenname")
RS2.MoveNext
Wend
RS2.Close()
Set RS2 = Nothing
%>

The above works really well for a single user specified in sLogonDomain, say my own account. What i need to do is pull out the info for all the users in group such as 'Marketing'. If i specify 'Marketing' in sLogonDomain the ASP doesn't report any errors but no data comes out. Can anyont point me in the right direction?

Thanks.
0
MrPrince
Asked:
MrPrince
  • 11
  • 9
  • 7
2 Solutions
 
sirbountyCommented:
Your select statement isolates this to the one user...(where samaccountname=...)

strRS2 = "SELECT givenname,sn,title,telephoneNumber,mail FROM 'LDAP://" & SDomain &"' WHERE sAMAccountName = '"&sLogonDomain &"' ORDER by name "

remove that and perform testing against each one, or if you're referring to the Marketing OU, change sDomain to "ou=marketing,dc=domain,dc=com"
0
 
MrPrinceAuthor Commented:
Removing the Where statement causes errors with ASP.

strRS2 = "SELECT givenname,sn,title,telephoneNumber,mail FROM 'LDAP://" & SDomain & sLogonDomain &"' ORDER by name "

Also if i specify the 'Marketing' OU then only users from that OU will be searched. I have several groups in my users OU that need to be searched not at the same time though. For example the page might have a drop list running by department and an all users option at then end. If the all users seclection is pressen then the search should get all  info from staff members in that group. All groups are in my users OU. Hope that makes sense.
0
 
sirbountyCommented:
Use
strRS2 = "SELECT givenname,sn,title,telephoneNumber,mail FROM 'LDAP://" & SDomain

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
MrPrinceAuthor Commented:
Tried

strRS2 = "SELECT givenname,sn,title,telephoneNumber,mail FROM 'LDAP://" & SDomain

With sDomain as :

domain.co.uk
ou=marketing,dc=domain,dc=co,dc=uk
domain.co.uk/ou=marketing,dc=empiremuseum,dc=co,dc=uk

ASP throws up "Provider error '80040e14'"
0
 
sirbountyCommented:
strRS2 = "SELECT givenname,sn,title,telephoneNumber,mail FROM 'LDAP://dc=domain,dc=co,dc=uk'"
0
 
sirbountyCommented:
Make sure the LDAP section is enclosed in ' (apostrophes)
0
 
MrPrinceAuthor Commented:
OK nearly there i think, ASP now throws up "Table does not exist." How do i reference the group 'Staff contatcs list'?
0
 
sirbountyCommented:
table does not exist means it's not finding what you've got there...permissions or syntax problems, more than likely...
0
 
NovoNordiskCommented:
What information do you want to get?  If its just the usernames of those in the group then its much easier to use the winnt:// protocol like below:
    Dim Group
    Dim User
    Set Group = GetObject("WinNT://" & strDomain & "/" & strGroup & ",group")
    For Each User in Group.Members
      Response.Write User.Name
    Next

If you want more information about each user then you need to use the ldap:// protocol like so:

Set objGroup = GetObject("LDAP://CN=Managers,OU=Finance,DC=fabrikam,DC=com")

For Each objUser in objGroup.Members
    response.write "Name: " & objUser.DisplayName & " First name: "& objUser.givenname & " Surname: "& objUser.sn & "<br>"
Next
 
The only thing you need to do is enter the location of the group you wish to query.
0
 
MrPrinceAuthor Commented:
Thanks for your reply. Your suggestion certainly looks far neater than my example. I am however slightly confused about referencing groups with LDAP. My group is not located within any special OU just the Users one. Using your LDAP example would I simply write?

LDAP://CN=Staff ContactsOU=Users,DC=fabrikam,DC=com
0
 
NovoNordiskCommented:
What is your group called?
0
 
MrPrinceAuthor Commented:
Staff Contacts, among others.
0
 
NovoNordiskCommented:
Then yes assuming its simply in the users container it would be:

LDAP://CN=Staff Contacts,OU=Users,DC=fabrikam,DC=com

To be sure you can right click the group in active directory and go to object to see its path
0
 
MrPrinceAuthor Commented:
Wicked thanks, I'll check it our tomorrow when i'm at work.
0
 
sirbountyCommented:
Actually Users is a container as well (and you're missing a comma there).
Try this:

LDAP://CN=Staff Contacts,CN=Users,DC=fabrikam,DC=com
0
 
sirbountyCommented:
Left this open and didn't refresh first...so the comma statement is bogus, but the container for Users still needs to be altered..
0
 
MrPrinceAuthor Commented:
Using the code:

Set objGroup = GetObject("LDAP://CN=Managers,OU=Finance,DC=fabrikam,DC=com")

For Each objUser in objGroup.Members
    response.write "Name: " & objUser.DisplayName & " First name: "& objUser.givenname & " Surname: "& objUser.sn & "<br>"
Next

I get ASP error '80005000'. As far as I know an '80005000' error is an 'invalid path' error. If I run the script from Windows:

Set objGroup = GetObject("LDAP://CN=Staff Contacts List,CN=Users,DC=empiremuseum,DC=co,DC=uk")

For Each objUser in objGroup.Members
    msgbox objUser.DisplayName
Next

It works fine. Any ideas about the '80005000'? Thanks.
0
 
NovoNordiskCommented:
What do you mean if you run it from windows it works?
0
 
MrPrinceAuthor Commented:
As a vbs file.
0
 
NovoNordiskCommented:
Well in that case it probably means what ever user your ASP pages are running as does not have enough rights to query active directory.  If you are using IIS with an anonymous account try changing it to a different account and see if that works
0
 
MrPrinceAuthor Commented:
Wicked. Very nearly there. How would iIorder my list by givenname?
0
 
sirbountyCommented:
You would have to build an ArrayList/SortedList with your detail and sort it by that field...I'm fairly certain that you can't simply drop an "Order By" clause in the command statement...
0
 
NovoNordiskCommented:
Yes you need an array like:

Dim arrNames()
intSize = 0

Set objGroup = GetObject("LDAP://CN=Accountants,OU=Finance,DC=fabrikam,DC=com")

For Each strUser in objGroup.Member
    Set objUser =  GetObject("LDAP://" & strUser)
    ReDim Preserve arrNames(intSize)
    arrNames(intSize) = objUser.givenName
    intSize = intSize + 1
Next

For i = (UBound(arrNames) - 1) to 0 Step -1
    For j= 0 to i
        If UCase(arrNames(j)) > UCase(arrNames(j+1)) Then
            strHolder = arrNames(j+1)
            arrNames(j+1) = arrNames(j)
            arrNames(j) = strHolder
        End If
    Next
Next

For Each strName in arrNames
    Response.write strName & "<br>"
Next

For more info see http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov04/hey1122.mspx
0
 
MrPrinceAuthor Commented:
Many thanks for the help from both of you. It works great now. No more multiple updates!
0
 
NovoNordiskCommented:
Hmmm so although I provided an exact answer I only got assisted!?
0
 
sirbountyCommented:
Glad I could assist you.
Thanx.
0
 
MrPrinceAuthor Commented:
NovoNordisk, I clicked on your answer first, so i'm not sure why it was assigned as assisted. It should be the other way around, although i decided to equally award points - not to take anything away from sirbounty.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 11
  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now