Brian Redmond's Weblog

Good IT Stuff

Blogs

Using the jpegPhoto attribute in AD - Part II

  • Comments 4
  • Likes

The previous post (http://weblogs.asp.net/btrst4/archive/2004/08/30/222629.aspx) on this topic showed you how to import picture data into AD. This post shows you how to retieve the data and write it back out to a file. It might be more realistic to retrieve the data from AD and display it in a portal. I'm sure someone could adapt this code to do this quite easily.  If you know how, please post or email me.

Basically, this is the reverse of the previous code using System.DirectoryServices then System.IO. 

Here is the code:
Imports System.IO
Imports System.DirectoryServices

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

        strFileName = "c:\PictureExportedFromAD.jpg"

        '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)

        'Retrieve picture into byte array
        Dim byteArray As Byte()
        byteArray = myUser.Properties("jpegPhoto").Value
        myUser.Close()

        'Open file to export (delete if exists)
        If File.Exists(strFileName) Then
            File.Delete(strFileName)
        End If

        outFile = New System.io.FileStream(strFileName, FileMode.CreateNew, FileAccess.ReadWrite)

        'Loop through bytes and write to file
        Dim singleByte As Byte

        For Each singleByte In byteArray
            outFile.WriteByte(singleByte)
        Next
        outFile.Close()
    End Sub
End Module

Comments
  • Ok - I have been able to import a photo into this attibute, but I have a question.

    Is there anywhere that this image will display within the properties of AD? I can see the usefulness of having the attribute in a users properties, but how is it viewable?

    I understand that the code you have above will retrieve the info from the AD and write it out to a file, but what if you just want to view the data stored in that attribute without an output file?

    Any ideas on this would be great!!

    Thanks!

  • Good question Naomi. Two ways you might want to display this data. One is via a web page. I don't have this code, but I bet it is not too hard.

    The other option is to display the picture in Active Directory Users & Computers. This can be done with "display specifiers" and extensions to ADUC. Look in MSDN and you will see information on how to add custom tabs to ADUC and display custom fields.

  • Easier said then done.

    I am aware of the display specifiers, and I understand what they do. The issue I am having is that in order to add a tab or a field to display the JPEG for each user, it seems that a COM object must be written and it must be done in C++ according to the articles I have read. I dont have any issue with using ADSIEdit to mosify the display specifiers, but it just seems like there is way more to it then that.

    I wonder if you are aware of any tools that would make the process easier for someone like me who is not a programmer.

    Thanks!

  • I agree that this is not easy. Unfortunately, the only way to do this that I am aware of is creating the COM object. I did not think you needed C++ and there are samples, but there is no way to avoid the issue.