I have a page on which I want to show pictures of some users. The picture needs to be read from Active Directory.

The user whose image will be shown is not logged in user but some other users. I know that to read image of those users we need some kind of ID so that we can identify which image belongs to which user. Suppose there is a field in AD called EmployeeID and I want to display pictures of users having EmployeeID between 1 and 10.

How to do that?



Here is the code to fetch picture from Active Directory..

static Image GetUserPicture(string employeeId)
   using (DirectorySearcher dsSearcher = new DirectorySearcher())
      dsSearcher.Filter = "(&(objectClass=user) (employeeId=" + employeeId + "))"; //employeeId is the custom column name
      SearchResult result = dsSearcher.FindOne();

      using (DirectoryEntry user = new DirectoryEntry(result.Path))
          byte[] data = user.Properties["jpegPhoto"].Value as byte[];

          if (data != null)
             using (MemoryStream s = new MemoryStream(data))
                 return Bitmap.FromStream(s);

          return null;

Retrieve Active Directory User Photos in C#


Do you have already configured User profile synchronization?

In this situation - you must have certainly also mapped in the profile properties to extract the image from AD (usually stored in thumbnailPhoto ) (if any). In this case you must know that all images (in 3 sizes) are automatically stored in an image library (see here http://blogs.msdn.com/b/spsocial/archive/2011/01/07/photo-management-in-sharepoint-2010.aspx) in the My Sites Host site. For your information you could use an "Export" property mapping to allow users to upload their photo in SharePoint and have it uploaded automatically in AD.

So far, is all out-of-the-box configuration, no-code involved so far! From here you could simply construct the Url to any of the 3 image sizes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.