Click here to Skip to main content
15,883,737 members
Please Sign up or sign in to vote.
3.46/5 (3 votes)
Hey guys, i am very sad right now cuz i cant figure out how to get a AD-User Picture into my SQL Database by using an Webservice.

What i really need is a WebService fetching the jpegPhoto attribute from AD convert it into a byte array and save into the database.

I've already done something like this:

C#
public bool SyncUser(string UserName)
{
   bool synced = true;

   DirectoryEntry oEntry = new DirectoryEntry("LDAP://" +  Properties.Settings.Default.ActiveDirectory_sDomain);

   DirectorySearcher oSearch = new DirectorySearcher(oEntry);
   oSearch.Filter = "(&(objectClass=user)(userPrincipalName=" + UserName + "))";
   SearchResult oResult = oSearch.FindOne();
   DirectoryEntry oUser = new DirectoryEntry(oResult.Path);
   PropertyValueCollection oCol = oUser.Properties["displayName"];

   DataTable dtData = new DataTable();
   SqlConnection oSQLConnection = new SqlConnection(<con-string>);


   //Image img = GetUserPicture(UserName);
   //byte[] UserPicture = ImageToByte(img);
   //FunctionInsertPictureIntoDB(UserPicture, "image/jpeg", UserName);


   string sQuery = "UPDATE tblUser SET " +
"General_FirstName='" + oUser.Properties["givenName"].Value + "', " +
"General_LastName='" + oUser.Properties["sn"].Value + "', " +
"General_DisplayName='" + oUser.Properties["displayName"].Value + "', " +
"General_Description='" + oUser.Properties["description"].Value + "', " +
"General_Office='" + oUser.Properties["physicalDeliveryOfficeName"].Value + "', " +
"General_EMail='" + oUser.Properties["mail"].Value + "', " +
"General_WebPage='" + oUser.Properties["wWWHomePage"].Value + "', " +
"Organization_JobTitle='" + oUser.Properties["title"].Value + "', " +
"Organization_Department='" + oUser.Properties["department"].Value + "', " + "Organization_Company='" + oUser.Properties["company"].Value + "', " +
"Organization_Manager='" + oUser.Properties["manager"].Value + "', " +
"Address_Street='" + oUser.Properties["streetAddress"].Value + "', " +
"Address_POBox='" + oUser.Properties["postOfficeBox"].Value + "', " +
"Address_City='" + oUser.Properties["l"].Value + "', " +
"Address_StateProvince='" + oUser.Properties["st"].Value + "', " +
"Address_ZipPostalCode='" + oUser.Properties["postalCode"].Value + "', " +
"Address_CountryRegion='" + oUser.Properties["co"].Value + "', " +
"Telephone_Home='" + oUser.Properties["homePhone"].Value + "', " +
"Telephone_Work='" + oUser.Properties["telephoneNumber"].Value + "', " +
"Telephone_Mobile='" + oUser.Properties["mobile"].Value + "', " +
"Telephone_Fax='" + oUser.Properties["facsimileTelephoneNumber"].Value + "', " +
"ActiveDirectory_ID='" + oUser.Guid + "', " +
"ActiveDirectory_DN='" + oUser.Path.Substring(oUser.Path.LastIndexOf("/") + 1) + "', " +
"Account_UserPrincipalName='" + oUser.Properties["userPrincipalName"].Value + "'" +
"IsInSyncWithAD='True' " +
"WHERE Account_UserPrincipalName='" + UserName + "'";


   SqlCommand cmd = new SqlCommand(sQuery, oSQLConnection);
   SqlDataAdapter da = new SqlDataAdapter(cmd);

   try
   {
      oSQLConnection.Open();
      da.Fill(dtData);
      da.Dispose();

   }
   catch
   {
      synced = false;
   }
   finally
   {
      oSQLConnection.Close();
   }

   return synced;
}


private Bitmap GetUserPicture(string Username)
{
   using (DirectoryEntry user = new DirectoryEntry("LDAP://" + Properties.Settings.Default.ActiveDirectory_sDomain))
   {
      using (MemoryStream s = new MemoryStream(user.Properties["jpegPhoto"].Value as byte[]))
      {
         return Bitmap.FromStream(s) as Bitmap;
      }
   }
}

private byte[] ImageToByte(Image img)
{
   MemoryStream ms = new MemoryStream();
   img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
   byte[] BytePicture = ms.ToArray();
   img.Dispose();
   ms.Close();
   return BytePicture;
}

private void FunctionInsertPictureIntoDB(byte[] UserPicture, string Type, string Username)
{
   SqlConnection oSQLConnection = new SqlConnection(<con-string>);
   SqlCommand cmd = new SqlCommand();

   cmd.Connection = oSQLConnection;
   cmd.CommandType = CommandType.Text;
   cmd.CommandText = "UPDATE tblUser SET PictureType=@ContentType, Picture=@Data WHERE Account_UserPrincipalName='" + Username + "'";

   SqlParameter ContentTypeParameter = new SqlParameter("@ContentType", SqlDbType.VarChar);
   SqlParameter DataParameter = new SqlParameter("@Data", SqlDbType.Binary);

   cmd.Parameters.Add(ContentTypeParameter);
   cmd.Parameters.Add(DataParameter);

   ContentTypeParameter.Value = Type;
   DataParameter.Value = UserPicture;

   SqlDataAdapter SDA = new SqlDataAdapter();

   try
   {
      oSQLConnection.Open();
      cmd.ExecuteNonQuery();
   }
   catch (System.Data.SqlClient.SqlException ex)
   {
      throw new Exception(ex.Message);
   }
   finally
   {
      oSQLConnection.Close();
   }
}
}


So this is what i have - some ideas? ;)
Posted
Updated 13-May-14 3:31am
v3
Comments
ZurdoDev 13-May-14 21:25pm    
So where are you stuck?
scnnx 14-May-14 2:19am    
The problem is that my compiler tells me that the buffer can't be null and i dont know how to get the binary data from the Image Object to insert it into my database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900