Using the jpegPhoto attribute in AD - Part I

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:  https://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