Last post Aug 15, 2008 05:07 AM by ssg31415926
Jul 24, 2008 08:34 AM|wkwork|LINK
Hey guys, I'm trying to query AD for the number of users in each OU. Unfortunately the AD tree is broken up into multiple levels so I can just look in one spot. So I'm pulling a list of all users and using their distinguished name to parse out the OU (which
is our 3-digit office code for each office). Here's the (preliminary) code that returns a list of users and their OUs:
Shared Function ADUserCountByOU() As SortedList
Dim TempList As New SortedList
Dim ResultList As System.DirectoryServices.SearchResultCollection
Dim N As Integer
ResultList = Database.ADQueryResult("(&(objectCategory=person)(objectClass=user))")
For N = 0 To 19 'ResultList.Count - 1
' Use the DN to get the users office code.
' DN is "CN=Name, OU=users, OU=office,..." etc.
Dim Temp As Object
Temp = Split(.Properties("distinguishedName").ToString, "=")
Temp = Split(Temp(3), ",")
'If the OU has less than 5 characters, it's a
'valid office code. Add it to the list.
If Temp(0).ToString.Length < 5 Then
This works. If I break at line 27 I can see that TempList contains a list of 1000 entries with the sAMAccount name and the OU. But on the page, I tried to bind a gridview to this function in the OnLoad event and it always comes up blank. I tried a datalist and got the same thing.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DataList1.DataSource = BusinessObjects.Database.ADUserCountByOU
Of course I need to find out how i can increase the max size to more than 1000 so I get all users and come up with a way to count them instead of list them, but first things first. Anyone see why I can't display this sorted list?
Aug 15, 2008 05:07 AM|ssg31415926|LINK
How about a two-part query: first retrieve a list of OUs (objectCategory=organizationalUnit) and then iterate over it retrieving a count of users.
And add .PageSize = 1000; to your DirectorySearcher object. This will instruct AD to carry out a paged search. It will only return 1000 objects at a time, but you can tell it to send multiple pages of 1000 objects.