Below is my code. I want to retrieve all the users whose first name or last name is same in a grid. But here am getting the name itself in the grid sorted.
I’m giving user a choice to enter user name. Once he enter the name I should be able to search in Active Directory and return all user starting with that text entered by the user.
I should be able to display all possibilities, for example if user enters adam I should give him choice to select whether he want to see adam josef or adam john e.t.c.
Any suggestion will be helpful.
Here is the code
DirectoryEntry de = new DirectoryEntry("ADConnection");
DirectorySearcher deSearch = new DirectorySearcher(de);
//set the search filter
deSearch.SearchRoot = de;
String UserName = txt_To.Text;
deSearch.Filter = "(&(objectCategory=user)(GivenName=*" + UserName + "*))";
string[] arrPropertiesToLoad = { "sn" };
deSearch.PropertiesToLoad.AddRange(arrPropertiesToLoad);
SearchResultCollection sResultColl = deSearch.FindAll();//Getting undefined error
Gridview1.DataSource = sResultColl ;
Gridview1.DataBind();
Here is the stack trace
[COMException (0x80004005): Unspecified error]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +439513
System.DirectoryServices.DirectoryEntry.Bind() +36
System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +78
System.DirectoryServices.DirectorySearcher.FindAll() +9
Certificate.WebForm4.btngo0_Click(Object sender, EventArgs e) in
C:\Users\273714\documents\visual studio
2010\Projects\Certificate\Certificate\WebForm4.aspx.cs:202
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
+36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+5563
You can use a
PrincipalSearcherand a “query-by-example” principal to do your searching:If you haven’t already – absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in
System.DirectoryServices.AccountManagement. Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace.Of course, depending on your need, you might want to specify other properties on that “query-by-example” user principal you create:
DisplayName(typically: first name + space + last name)SAM Account Name– your Windows/AD account nameUser Principal Name– your “username@yourcompany.com” style nameYou can specify any of the properties on the
UserPrincipaland use those as “query-by-example” for yourPrincipalSearcher.Update: if you want to find a bunch of users and bind those to a gridview, use this code:
Don’t iterate twice over all the data returned! (as in your comment
) …..
Update #2: with the S.DS.AM classes, you always get the complete class – there’s nothing you can do about this. If you want to select only specific LDAP attributes, you need to use your original approach.
From that approach, you need to make sure to create the root of the
DirectorySearcheron a container – e.g. theOU=Userscontainer – NOT on an individual AD object like a specific user.So try using this code:
Does that work?