locked
C# code to add and retrieve user photos from active directory RRS feed

  • Question

  • Hi all,

     

    Can anyone tell me how to add or retrieve user pictures from active directory using c# code.

    Tuesday, May 24, 2011 7:23 PM

Answers

  • I don't know exactly, why it happened. You can try below code:

    static void InsertPicture(string userName, byte[] data)
    {
      using (DirectorySearcher dsSearcher = new DirectorySearcher())
      {
        dsSearcher.Filter = "(&(objectClass=user) (cn=" + userName + "))";
        SearchResult result = dsSearcher.FindOne();
    
        using (DirectoryEntry user = new DirectoryEntry(result.Path))
        {
          user.Properties["jpegPhoto"].Clear();
          user.Properties["jpegPhoto"].Add(data);
          user.CommitChanges();
        }
      }
    }
    
    static Image GetUserPicture(string userName)
    {
      using (DirectorySearcher dsSearcher = new DirectorySearcher())
      {
        dsSearcher.Filter = "(&(objectClass=user) (cn=" + userName + "))";
        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;
        }
      }
    }
    


    Make everything as simple as possible, but not simpler.
    • Marked as answer by Aspen VJ Thursday, June 2, 2011 2:05 AM
    Friday, May 27, 2011 11:08 AM

All replies

  • Here's an use example:

     

    // User Distinguished Name, "CN=?,OU=Users"
    // Domain Controller, ie: DC01
    static void InsertPicture(string userName, string userPictureFileName)
    {
     using (DirectoryEntry user = new DirectoryEntry(string.Format("LDAP://DC01/CN={0},OU=Users", userName)))
     {
      user.Properties["jpegPhoto"].Clear();
      user.Properties["jpegPhoto"].Add(File.ReadAllBytes(userPictureFileName));
      user.CommitChanges();
     }
    }
    
    static Image GetUserPicture(string userName)
    {
     using (DirectoryEntry user = new DirectoryEntry(string.Format("LDAP://DC01/CN={0},OU=Users", userName)))
    { using (MemoryStream s = new MemoryStream(user.Properties["jpegPhoto"].Value as byte[])) { return Bitmap.FromStream(s); } } }

     


    Make everything as simple as possible, but not simpler.
    Tuesday, May 24, 2011 8:40 PM
  • Here I actully we are using "thumbnailphoto" if use the same code it is giving me the error A constraint violation occurred. (Exception from HRESULT: 0x8007202F).. Can you assist me regarding this issue please
    Friday, May 27, 2011 7:26 AM
  • I don't know exactly, why it happened. You can try below code:

    static void InsertPicture(string userName, byte[] data)
    {
      using (DirectorySearcher dsSearcher = new DirectorySearcher())
      {
        dsSearcher.Filter = "(&(objectClass=user) (cn=" + userName + "))";
        SearchResult result = dsSearcher.FindOne();
    
        using (DirectoryEntry user = new DirectoryEntry(result.Path))
        {
          user.Properties["jpegPhoto"].Clear();
          user.Properties["jpegPhoto"].Add(data);
          user.CommitChanges();
        }
      }
    }
    
    static Image GetUserPicture(string userName)
    {
      using (DirectorySearcher dsSearcher = new DirectorySearcher())
      {
        dsSearcher.Filter = "(&(objectClass=user) (cn=" + userName + "))";
        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;
        }
      }
    }
    


    Make everything as simple as possible, but not simpler.
    • Marked as answer by Aspen VJ Thursday, June 2, 2011 2:05 AM
    Friday, May 27, 2011 11:08 AM
  • in my case string constants are different from were specified

    public static Image GetUserPicture(string userName)
    {
    	using (var dsSearcher = new DirectorySearcher())
    	{
    		var idx = userName.IndexOf('\\');
    		if (idx > 0)
    			userName = userName.Substring(idx + 1);
    		dsSearcher.Filter = string.Format("(&(objectClass=user)(samaccountname={0}))", userName);
    		SearchResult result = dsSearcher.FindOne();
    		if (result != null)
    		{
    			using (var user = new DirectoryEntry(result.Path))
    			{
    				var data = user.Properties["thumbnailPhoto"].Value as byte[];
    
    				if (data != null)
    					using (var s = new MemoryStream(data))
    						return Bitmap.FromStream(s);
    				else
    					foreach (PropertyValueCollection p in user.Properties)
    						Trace.WriteLine(p.PropertyName);
    			}
    		}
    		return null;
    	}
    }
    


    Regards

    Wednesday, September 3, 2014 8:23 AM