Hello I want help on Active Directory, I am trying build query Active Directory for every users on beneath present manager that is signed in. so I just wanted to create code for this, so can anyone help how to do this…. Thanks in advance.
Hello I want help on Active Directory, I am trying build query Active Directory for every users on beneath present manager that is signed in. so I just wanted to create code for this, so can anyone help how to do this…. Thanks in advance.
You can try this and I hope this will help you to solve your issue:
Code:private static Collection<string> GetDirectReportsInternal(string ldapBase, string userDN, out long elapsedTime) { Collection<string> result = new Collection<string>(); Stopwatch sw = new Stopwatch(); sw.Start(); string principalname = string.Empty; using (DirectoryEntry directoryEntry = new DirectoryEntry(ldapBase)) { using (DirectorySearcher ds = new DirectorySearcher(directoryEntry)) { ds.SearchScope = SearchScope.Subtree; ds.PropertiesToLoad.Clear(); ds.PropertiesToLoad.Add("userPrincipalName"); ds.PropertiesToLoad.Add("distinguishedName"); ds.PageSize = 10; ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2); ds.Filter = string.Format("(&(objectCategory=user)(manager={0}))",userDN); using (SearchResultCollection src = ds.FindAll()) { Collection<string> tmp = null; long subElapsed = 0; foreach (SearchResult sr in src) { result.Add((string)sr.Properties["userPrincipalName"][0]); tmp = GetDirectReportsInternal(ldapBase, (string)sr.Properties["distinguishedName"][0], out subElapsed); foreach (string s in tmp) { result.Add(s); } } } } } sw.Stop(); elapsedTime = sw.ElapsedMilliseconds; return result; }
Hello thanks for the replies but I am still hard work around with above code, first of all I am using VB.Net program to create code and I am looking for code or query Active Directory for all users on particular manager and after that just bind all query into gridview. I have looked at your code but this doesn’t helped me. Can anyone provide me example how this should work.
Ok I will give example for your question, if you will want to sort for any candidate (for .e.g. Manager)
This will sort all manger’s name distinguished in the directory. So to do this you need to have manager's same accountname and you need to do distinguished name query for his/her before searching for list as manager. Here e.g. code to pull manager with distinguishedname and you can use this code on your function to load managed employeesCode:(&(objectCategory=User)(manager=CN=Raj\, Shrivastav))
hope this will help you to solve your issue.Code:Dim ManagerDNstr as string = GetManagerName("shrivastav.raj ") search.filter = "(&(objectCategory=User)(manager=" & ManagerDNstr & "))"
Ok I am using below and I hope his helps you to solve your issue:
Code:Dim Etry As New System.DirectoryServices.DirectoryEntry("LDAP://domain") Dim Searcher As DirectorySearcher = New DirectorySearcher(Etry ) Dim Results As SearchResultCollection Etry.Username = Nothing Etry.Password = Nothing With oSearcher .Filter = ("(&(manager=some name))") End With Searcher.PropertiesToLoad.Add("givenName") Searcher.PropertiesToLoad.Add("sn") Searcher.PropertiesToLoad.Add("sAMAccountname") Searcher.PropertiesToLoad.Add("physicalDeliveryOfficeName") Searcher.PropertiesToLoad.Add("department") Searcher.PropertiesToLoad.Add("manager") Dim Groupcol As ArrayList Groupcol = New ArrayList Results = Searcher.FindAll 'Dim Members As Object = Searcher.FindAll If Results IsNot Nothing Then For Each Result In Results Dim strFName As String = Result.Properties("givenName")(0).ToString Dim strLName As String = Result.Properties("sn")(0).ToString Groupcol.Add(strFName + " " + strLName) 'Groupcol.Add(Result.Properties("givenName")(0).ToString & " " & Result.Properties("sn")(0).ToString) Next End If Groupcol.Sort() '<<< Sort by ascending ddlReportsTo.DataSource = Groupcol(0) '<<< Bind to Dropdown List ddlReportsTo.DataBind() Searcher.Dispose()
Getting the info to display will be the easy part. Since you have the data in a table you can use it just like any other data table and bind it to a data grid or drop down list. If you want to sort it, assert a dataset and insert the table to it then you can arrange it. If you need an example I can post some code tomorrow.
Bookmarks