Displaying thumbnailPhoto from Active Directory on a web page

In my Exchange 2010 environment, we’re using the thumbnailPhoto attribute in active directory, so that users photos appear in Outlook 2010. We also have a custom intranet page that features a company directory which pulls details such as phone numbers from AD. I wanted to also add in the image stored in thumbnailPhoto for each user, so started to create a way of extracting the image that could be used in a web page.

I ended up with a C# ASP page, called userPhoto.aspx with the following contents:

<%@ Page Language="C#" %>
<%@ OutputCache Duration="6000" VaryByParam="u" %>
<%@ Import Namespace="System.DirectoryServices" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    private void Page_Load(object sender, EventArgs e)
    {
        String myUser = Request.QueryString["u"];
        
        if (myUser == null)
            Response.Redirect("app_graphics/user.jpg");

        Response.ContentType = "image/jpeg";
        Response.Clear();
        Response.BufferOutput = true;
        
        DirectoryEntry de = new DirectoryEntry();
        de.Path = "LDAP://OU=Users,DC=domain,DC=local";

        DirectorySearcher search = new DirectorySearcher();
        search.SearchRoot = de;
        search.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + myUser + "))";
        search.PropertiesToLoad.Add("samaccountname");
        search.PropertiesToLoad.Add("thumbnailPhoto");

        SearchResult user;
        user = search.FindOne();

        String userName;
        if (user == null)
            Response.Redirect("app_graphics/user.jpg");
        else
            userName = (String)user.Properties["sAMAccountName"][0];

        try
        {
            byte[] bb = (byte[])user.Properties["thumbnailPhoto"][0];
            Response.BinaryWrite(bb);
            Response.Flush();
        }
        catch
        {
            Response.Redirect("app_graphics/user.jpg"); 
        }

    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>userImage</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

You can then use the file by passing a parameter of u on a HTTP GET request, containing the users sAMAccountName.

For example, <img src=’userPhoto.aspx?u=gkendal’/>. Also, don’t forget to set the OU of where your user objects are in AD on line 21, and to add a placeholder image for those users that dont have anything stored in thier thumbnailPhoto attribute!

2 Comments

  • Paul Beynon commented on September 21, 2012

    I am trying to get the company directory part working on an internal intranet for our company, possibly with the Photos at a later date, but I cannot get a company direcory to work at all – can you put the code for that part up too?

    Thanks

  • Geoff Kendal commented on September 26, 2012

    Hi Paul, Not sure if this will paste well… Here goes:

    protected void Page_Load(object sender, EventArgs e)
    {

    DataTable userList = new DataTable();
    DataColumn userColumn;

    userColumn = new DataColumn();
    userColumn.DataType = Type.GetType(“System.String”);
    userColumn.ColumnName = “First”;
    userList.Columns.Add(userColumn);

    userColumn = new DataColumn();
    userColumn.DataType = Type.GetType(“System.String”);
    userColumn.ColumnName = “Last”;
    userList.Columns.Add(userColumn);

    userColumn = new DataColumn();
    userColumn.DataType = Type.GetType(“System.String”);
    userColumn.ColumnName = “Email”;
    userList.Columns.Add(userColumn);

    userColumn = new DataColumn();
    userColumn.DataType = Type.GetType(“System.String”);
    userColumn.ColumnName = “Extn”;
    userList.Columns.Add(userColumn);

    userColumn = new DataColumn();
    userColumn.DataType = Type.GetType(“System.String”);
    userColumn.ColumnName = “DDI”;
    userList.Columns.Add(userColumn);

    userColumn = new DataColumn();
    userColumn.DataType = Type.GetType(“System.String”);
    userColumn.ColumnName = “Mobile”;
    userList.Columns.Add(userColumn);

    userColumn = new DataColumn();
    userColumn.DataType = Type.GetType(“System.String”);
    userColumn.ColumnName = “Thumbnail”;
    userList.Columns.Add(userColumn);

    DirectoryEntry de = new DirectoryEntry();
    de.Path = “LDAP://OU=Users,OU=myorganisationalunit,DC=whatever,DC=com”;

    DirectorySearcher search = new DirectorySearcher();
    search.SearchRoot = de;
    search.Filter = “(&(objectClass=user)(objectCategory=person))”;
    search.PropertiesToLoad.Add(“samaccountname”);
    search.PropertiesToLoad.Add(“givenName”);
    search.PropertiesToLoad.Add(“sn”);
    search.PropertiesToLoad.Add(“mail”);
    search.PropertiesToLoad.Add(“ipPhone”);
    search.PropertiesToLoad.Add(“telephoneNumber”);
    search.PropertiesToLoad.Add(“mobile”);
    search.PropertiesToLoad.Add(“employeeType”);
    search.PropertiesToLoad.Add(“msExchHomeServerName”);

    SearchResult result;
    SearchResultCollection resultCol = search.FindAll();

    if (resultCol != null)
    {
    for (int counter = 0; counter < resultCol.Count; counter++) { result = resultCol[counter]; if (result.Properties.Contains("samaccountname") && result.Properties.Contains("mobile") && result.Properties.Contains("givenName") && result.Properties.Contains("sn")) { DataRow userRow; userRow = userList.NewRow(); userRow["First"] = (String)result.Properties["givenName"][0]; userRow["Last"] = (String)result.Properties["sn"][0]; try { string thisEmail; thisEmail = (String)result.Properties["mail"][0]; userRow["Email"] = "” + thisEmail.ToLower() + “”;
    thisEmail = null;
    }
    catch { }

    try { userRow[“Extn”] = (String)result.Properties[“ipPhone”][0]; }
    catch { }

    try { userRow[“DDI”] = (String)result.Properties[“telephoneNumber”][0]; }
    catch { }

    try { userRow[“Mobile”] = (String)result.Properties[“mobile”][0]; }
    catch { }

    try { userRow[“Thumbnail”] = ““; }
    catch { }

    userList.Rows.Add(userRow);

    }
    }
    }

    RadGrid1.DataSource = userList;
    RadGrid1.AutoGenerateColumns = true;
    RadGrid1.MasterTableView.SortExpressions.AddSortExpression(“Last ASC”);
    RadGrid1.DataBind();
    }

Leave a Reply

Your email address will not be published. Required fields are marked *