Please can somebody help I'm struggling to display the thumbnailPhoto attribute in Active Directory on my gridview website. Please find below the code and asp.net files <%@ Page Title="AD USERS" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" Inherits="_Default" Codebehind="Default.aspx.cs" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <table cellpadding="4" cellspacing="4" width="90%" align="center" style="border: 1px solid Green;"> <tr> <td> <asp:GridView ID="grdViewAllADSUsers" runat="server" AllowPaging="True" CellPadding="4" Width="98%" ForeColor="#333333" GridLines="None" OnPageIndexChanging="grdViewAllADSUsers_PageIndexChanging"> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <EditRowStyle BackColor="#999999" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#E9E7E2" /> <SortedAscendingHeaderStyle BackColor="#506C8C" /> <SortedDescendingCellStyle BackColor="#FFFDF8" /> <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> <Columns> <asp:ImageField DataImageUrlField="thumbnailPhoto" HeaderText="Image"> <ControlStyle Height="48px" Width="50px" /> </asp:ImageField> <asp:BoundField DataField="displayName" HeaderText="Name" /> <asp:BoundField DataField="title" HeaderText="Job title" /> <asp:BoundField DataField="mail" HeaderText="E-mail" /> <asp:BoundField DataField="telephonenumber" HeaderText="Telephone Number" /> <asp:BoundField DataField="mobile" HeaderText="Mobile Number" /> <asp:BoundField DataField="facsimileTelephoneNumber" HeaderText="Fax Number" /> <asp:BoundField DataField="department" HeaderText="Department" /> <asp:BoundField DataField="manager" HeaderText="Manager" /> <asp:BoundField DataField="st" HeaderText="Car Registration" /> </Columns> </asp:GridView> </td> </tr> </table> </asp:Content> C# code: public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) GetAllADUsers(); } public void GetAllADUsers() { try { DirectoryEntry myLdapConnection = createDirectoryEntry(); DirectorySearcher search = new DirectorySearcher(myLdapConnection); search.Filter = "(&(objectClass=user)(objectCategory=person)(!sAMAccountType=805306370)(!userAccountControl:1.2.840.113556.1.4.803:=2)(description=Exchange User))"; search.SearchScope = SearchScope.Subtree; search.PropertiesToLoad.Add("thumbnailPhoto"); search.CacheResults = true; SearchResultCollection allResults = search.FindAll(); DataTable resultsTable = new DataTable(); foreach (String colName in search.PropertiesToLoad) { if (colName == "thumbnailPhoto") resultsTable.Columns.Add(colName, typeof(System.Drawing.Bitmap)); else resultsTable.Columns.Add(colName,typeof(String)); } resultsTable.Columns.Remove("ADsPath"); foreach (SearchResult searchResult in allResults) { DataRow dr = resultsTable.NewRow(); if (searchResult.Properties.Contains("thumbnailPhoto")) { dr["thumbnailPhoto"] = byteArrayToImage((byte[])searchResult.Properties["thumbnailPhoto"][0]); //byte[]staffPic = ((byte[])searchResult.Properties["thumbnailPhoto"][0]); //Response.BinaryWrite(staffPic); //Response.Flush(); //dr["thumbnailPhoto"] = GetUserPicture(searchResult.Properties["samaccountname"][0].ToString()); } resultsTable.Rows.Add(dr); } grdViewAllADSUsers.DataSource = resultsTable; grdViewAllADSUsers.ShowHeader = true; grdViewAllADSUsers.AutoGenerateColumns = false; grdViewAllADSUsers.DataBind(); } catch (Exception) { } } public byte[] imageToByteArray(System.Drawing.Image imageIn) { MemoryStream ms = new MemoryStream(); imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); return ms.ToArray(); } public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn) { MemoryStream ms = new MemoryStream(byteArrayIn); System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms); return returnImage; } static System.Drawing.Image GetUserPicture(string userName) { using (DirectorySearcher dsSearcher = new DirectorySearcher()) { dsSearcher.Filter = "(&(objectClass=user) (cn=" + userName + "))"; SearchResult result = dsSearcher.FindOne(); using (DirectoryEntry user = new DirectoryEntry(result.Path)) { byte[] data = user.Properties["thumbnailPhoto"][0] as byte[]; if (data != null) { using (MemoryStream s = new MemoryStream(data)) { return System.Drawing.Bitmap.FromStream(s); } } return null; } } } static DirectoryEntry createDirectoryEntry() { // create and return new LDAP connection with desired settings DirectoryEntry ldapConnection = new DirectoryEntry("my.domain"); ldapConnection.Path = "LDAP://my.domain/OU=Users,OU=Users & Computers,DC=my,DC=domain"; ldapConnection.AuthenticationType = AuthenticationTypes.Secure; return ldapConnection; } protected void grdViewAllADSUsers_PageIndexChanging(object sender, GridViewPageEventArgs e) { grdViewAllADSUsers.PageIndex = e.NewPageIndex; GetAllADUsers(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)