c# - Bind data from LDAP to asp.repeater control -


i have repeater control , wish populate data ldap. can connect active directory , return list of 600 plus names, when try , bind data returns following message:

system.directoryservices.searchresult' not contain property name 'name'

i dont want return name, email, phone, jobtitle , few other fields.

is there way can this?

this c# code:

        private void getallusers()     {         searchresultcollection results;         directorysearcher ds = null;         directoryentry de = new directoryentry(getcurrentdomainpath());         ds = new directorysearcher(de);         ds.propertiestoload.add("name");         ds.filter = "(&(objectcategory=user)(objectclass=person))";         results = ds.findall();         rptad.datasource = results;         rptad.databind();         foreach (searchresult sr in results)         {             debug.writeline(sr.properties["name"][0].tostring());         }     } 

and code repeater:

<asp:content id="bodycontent" contentplaceholderid="maincontent" runat="server"> <div class="container">     <h2>ad users</h2>     <asp:repeater id="rptad" runat="server">         <headertemplate>             <table class="table table-striped table-bordered">                 <tr>                     <td>ad user</td>                 </tr>         </headertemplate>         <itemtemplate>                 <tr>                     <td>                         <%# databinder.eval(container.dataitem,"name") %>;                     </td>                 </tr>         </itemtemplate>         <footertemplate>             </table>         </footertemplate>     </asp:repeater> </div> 

it´s been while since used library think of properties not mapped. anyways if trying full name try property ["displayname"]

here link question regarding properties available propertiestoload. 1 of answers contains links list of attributes. list long didn´t check didn´t find quick search.

as returning more 1 attribute load more properties did above 1 below other this:

ds.propertiestoload.add("samaccountname"); ds.propertiestoload.add("displayname"); ds.propertiestoload.add("userprincipalname"); ds.propertiestoload.add("objectsid"); 

and obtaining values same way well:

sr.properties["samaccountname"][0].tostring() sr.properties["displayname"][0].tostring() sr.properties["userprincipalname"][0].tostring() sr.properties["objectsid"][0].tostring() 

alternative

i suggest though if can, start using system.management.automation library obtaining ad. changed, , made life simpler because execute code powershell commands.

using (var powershell = powershell.create())             {                 var aduserslist = powershell                      .addcommand("get-aduser")                      .addparameter("ldapfilter", ldapquery)                      .addparameter("properties", new string[] { "emailaddress", "lockedout", "created", "modified" })                      .invoke();             } 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -