Updating Sharepoint 2010 UserProfile Pictures Programmatically

public void UploadPhoto(string url, string accountName, byte[] image)
{
  using (SPSite site = new SPSite(url))
  {
    using (SPWeb web = site.OpenWeb())
    {
     ProfileImagePicker profileImagePicker = new ProfileImagePicker();
     InitializeProfileImagePicker(profileImagePicker, web);
     SPFolder subfolderForPictures = GetSubfolderForPictures(profileImagePicker);

     string fileNameWithoutExtension = GetFileNameFromAccountName(accountName);

     int largeThumbnailSize = 120;
     int mediumThumbnailSize = 90;
     int smallThumbnailSize = 30;

     using (MemoryStream stream = new MemoryStream(image))
     {
       using (Bitmap bitmap = new Bitmap(stream, true))
       {
        CreateThumbnail(bitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_LThumb.jpg");
        CreateThumbnail(bitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_MThumb.jpg");
        CreateThumbnail(bitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_SThumb.jpg");
       }
      }
     }
   }
  }
  
  string GetFileNameFromAccountName(string accountName)
  {
    string result = accountName;
    string charsToReplace = @"/:*?""<>|";
    Array.ForEach(charsToReplace.ToCharArray(), charToReplace => result = result.Replace(charToReplace, '_'));
    return result;
  }

  void InitializeProfileImagePicker(ProfileImagePicker profileImagePicker, SPWeb web)
  {
    Type profileImagePickerType = typeof(ProfileImagePicker);

    FieldInfo fieldInfo = profileImagePickerType.GetField("m_objWeb", BindingFlags.NonPublic | BindingFlags.Instance);
    fieldInfo.SetValue(profileImagePicker, web);

    MethodInfo miLoadPictureLibraryInternal= profileImagePickerType.GetMethod("LoadPictureLibraryInternal", BindingFlags.NonPublic | BindingFlags.Instance);
   
    if (miLoadPictureLibraryInternal!= null)
    {
       miLoadPictureLibraryInternal.Invoke(profileImagePicker, new object[] { });
    }
   }

   SPFile CreateThumbnail(Bitmap original, int idealWidth, int idealHeight, SPFolder folder, string fileName)
   {
     SPFile file = null;
     Assembly userProfilesAssembly = typeof(UserProfile).Assembly;
     Type userProfilePhotosType = userProfilesAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfilePhotos");

     MethodInfo miCreateThumbnail = userProfilePhotosType.GetMethod("CreateThumbnail", BindingFlags.NonPublic | BindingFlags.Static);
     
     if (miCreateThumbnail != null)
     {
        file = (SPFile)miCreateThumbnail.Invoke(null, new object[] { original, idealWidth, idealHeight, folder, fileName });
     }

     return file;
   }

   SPFolder GetSubfolderForPictures(ProfileImagePicker profileImagePicker)
   {
     SPFolder folder = null;
     Type profileImagePickerType = typeof(ProfileImagePicker);

     MethodInfo miGetSubfolderForPictures = profileImagePickerType.GetMethod("GetSubfolderForPictures", BindingFlags.NonPublic | BindingFlags.Instance);
 
     if (miGetSubfolderForPictures != null)
     {
        folder = (SPFolder)mi_GetSubfolderForPictures.Invoke(profileImagePicker, new object[] { });
     }

     return folder;
 }

4 Thoughts on “Updating Sharepoint 2010 UserProfile Pictures Programmatically

  1. Ovais Khatri on 10/17/2012 at 16:44 said:

    I am facing an issue, it throws an exception of “Parameters count unmatched” on line
    folder = (SPFolder)mi_GetSubfolderForPictures.Invoke(profileImagePicker, new object[] { });

    The code is working on development server. But on production server, it not wokring.
    On development server, when ProfileImagePicker class object is created, its FolderName property has value “Profile Pictures”, but on production server it does not set any value for this property.

    Can you suggest any workaround ?

  2. I solved my issue by create an array of parameters and used mi_GetSubfolderForPictures.GetParameters() function, it returned me total number of parameters method was expecting , and in my case it was one, and it was asking for a bit whether to create the instance of folder if not created, I re-write this line folder = (SPFolder)mi_GetSubfolderForPictures.Invoke(profileImagePicker, new object[] {false }); , note the false boolean being passed as parameters and it worked for me nicley!!.

  3. bcakiroglu on 10/17/2012 at 19:30 said:

    Nice to hear that Ovais! :)

  4. agregator on 06/26/2013 at 19:53 said:

    Outstanding post, I conceive blog owners should learn a lot from this blog its really
    user genial . “My father always told me, Find a job you love and youll never have to work a day in your life.”
    by Jim Fox.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation