Brian Redmond's Weblog

Good IT Stuff

Blogs

Using the jpegPhoto attribute in AD - Part I

  • Comments 3
  • Likes

One of the big advantages of an Active Directory implementation is to store all of someone's personal data in their AD object. Nothing could be more personal than a photo. In many cases, companies already have pictures of users for ID badges, portals, etc.  It seems like it would make a lot of sense to store these photos in AD.

This is possible using the "jpegPhoto" attribute. In Windows 2000, the jpegPhoto attribute did not exist and there was something called "thumbnailPhoto."  The jpegPhoto attribute is more LDAP compliant and is the right one to use if you are on Windows 2003 AD.  AD reference:  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adschema/adschema/a_jpegphoto.asp

The picture is a binary blob, so you have to figure out how to convert the file data into binary and upload. Below is how I did it using the .NET Framework and Visual Basic .NET.  I will post how to retrieve the data in another post later.

How to insert picture data into AD
First, I used the System.IO class to import the file into an array of bytes. Then I connected to AD using System.DirectoryServices and inserted the data from the file.  When you are done, you will see it in LDP showing up as: jpegPhoto: <ldp: Binary blob>. 

Here is the code for the console application:
Imports System.IO
Imports System.DirectoryServices

Module Module1
    Sub Main()
        Dim inFile As System.IO.FileStream
        Dim binaryData() As Byte
        Dim strFileName As String

        'Picture file to open and import into AD
        strFileName = "C:\MyPicture.jpeg"

        'Open file
        inFile = New System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)

        'Retrive Data into a byte array variable
        ReDim binaryData(inFile.Length)
        Dim bytesRead As Long = inFile.Read(binaryData, 0, CInt(inFile.Length))
        inFile.Close()

        'Connect to AD
        Dim strDN As String = "CN=Joe User,OU=Employees,DC=company,DC=local"
        Dim strDCName As String = "DC-01"
        Dim myUser As New System.DirectoryServices.DirectoryEntry("LDAP://" & strDCName & "/" & strDN)

        'Clear existing picture if exists
        myUser.Properties("jpegPhoto").Clear()

        'Update attribute with binary data from file
        myUser.Properties("jpegPhoto").Add(binaryData)
        myUser.CommitChanges()

    End Sub
End Module

Comments
  • Having pisture in AD is OK. Question is, how do I persuade Exchange/Outlook enviroment to show this picture in Outlook? Exchange version is 2k (mixed).

    best regards, Ales

  • I am not sure where you are hoping to show this? The Outlook address book does not have a place to show a picture, so you cannot do this with a default setup. Maybe you could design an Outlook form for this?