Thursday 3 May 2012

How to upload a user profile photo programmatically

Updated No. 1: Changes required by SP 2010 RTM.

Updated No. 2: Method added to set the picture URL property for the user profile.

On the MSDN forum I’ve met a question that ask about how can one upload and set user profile pictures programmatically. I did a little research on the area and found another thread with the same question but I think the accepted answer of Chakkaradeep Chandran really does not answer the question, as it simply sets the image in the profile through the PictureUrl profile property, but does nothing related to upload and image resizing.

I admit the later tasks are not trivial although definitely not impossible through some hacking with Reflection. It is required since the methods we need (LoadPictureLibraryInternal, CreateThumbnail, GetSubfolderForPictures) are internal and private members of the ProfileImagePicker and UserProfilePhotos classes (later class is internal itself as well). So if you don’t mind this kind of dirty code, here is the solution for the question.

You should create a console application and add references to System.Drawing, System.Web, Microsoft.SharePoint, Microsoft.SharePoint.Portal, Microsoft.Office.Server and Microsoft.Office.Server.UserProfiles assemblies.

Next, use the code below:


  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Drawing;

  6. using System.Reflection;

  7. using Microsoft.SharePoint;

  8. using Microsoft.SharePoint.Portal.WebControls;

  9. using System.IO;

  10. using Microsoft.Office.Server.UserProfiles;

  11. namespace UploadProfileImage

  12. {

  13.     class Program

  14.     {

  15.         static void Main(string[] args)

  16.         {

  17.             Program prog = new Program();

  18.             prog.UploadProfileImages();

  19.         }

  20.         private void UploadProfileImages()

  21.         {

  22.             // my site host url

  23.             String url = "http://sp2010/profiles/host";

  24.             using (SPSite site = new SPSite(url))

  25.             {

  26.                 using (SPWeb web = site.OpenWeb())

  27.                 {

  28.                     ProfileImagePicker profileImagePicker = new ProfileImagePicker();

  29.                     InitializeProfileImagePicker(profileImagePicker, web);

  30.                     SPFolder subfolderForPictures = GetSubfolderForPictures(profileImagePicker);

  31.                     // repeat this block if you have more images and users

  32.                     // your account name

  33.                     String accountName = @"domain\administrator";

  34.                     // path to image file

  35.                     String imageFilePath = @"C:\Temp\AdminAvatar.jpg";

  36.                     UploadPhoto(accountName, imageFilePath, subfolderForPictures, profileImagePicker);

  37.                     SetPictureUrl(accountName, subfolderForPictures);

  38.                     // repeat block finished

  39.                 }

  40.             }

  41.         }

  42.         private void SetPictureUrl(string accountName, SPFolder subfolderForPictures)

  43.         {

  44.             Console.WriteLine("Setting profile image for user '{0}'", accountName);

  45.             SPSite site = subfolderForPictures.ParentWeb.Site;

  46.             UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.GetContext(site));

  47.             UserProfile userProfile = userProfileManager.GetUserProfile(accountName);

  48.             string fileNameWithoutExtension = GetFileNameFromAccountName(accountName);

  49.             string pictureUrl = String.Format("{0}/{1}/{2}_MThumb.jpg", site.Url, subfolderForPictures.Url, fileNameWithoutExtension);

  50.             userProfile["PictureUrl"].Value = pictureUrl;

  51.             userProfile.Commit();

  52.         }

  53.         private void UploadPhoto(string accountName, string imageFilePath, SPFolder subfolderForPictures, ProfileImagePicker profileImagePicker)

  54.         {

  55.             Console.WriteLine("Uploading image '{0}' for user '{1}'", imageFilePath, accountName);

  56.             if (!File.Exists(imageFilePath) || Path.GetExtension(imageFilePath).Equals(".gif"))

  57.             {

  58.                 Console.WriteLine("File '{0}' does not exist or has invalid extension", imageFilePath);

  59.             }

  60.             else

  61.             {

  62.                 string fileNameWithoutExtension = GetFileNameFromAccountName(accountName);

  63.                 FileStream file = File.Open(imageFilePath, FileMode.Open);

  64.                 BinaryReader reader = new BinaryReader(file);

  65.                 if (subfolderForPictures != null)

  66.                 {

  67.                     // try casting length (long) to int

  68.                     byte[] buffer = reader.ReadBytes((int)file.Length);

  69.                     int largeThumbnailSize = 0×90;

  70.                     int mediumThumbnailSize = 0×60;

  71.                     int smallThumbnailSize = 0×20;

  72.                     using (MemoryStream stream = new MemoryStream(buffer))

  73.                     {

  74.                         using (Bitmap bitmap = new Bitmap(stream, true))

  75.                         {

  76.                             CreateThumbnail(bitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_LThumb.jpg");

  77.                             CreateThumbnail(bitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_MThumb.jpg");

  78.                             CreateThumbnail(bitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_SThumb.jpg");

  79.                         }

  80.                     }

  81.                 }

  82.             }

  83.         }

  84.         private void InitializeProfileImagePicker(ProfileImagePicker profileImagePicker, SPWeb web)

  85.         {

  86.             Type profileImagePickerType = typeof(ProfileImagePicker);

  87.             FieldInfo fi_m_objWeb = profileImagePickerType.GetField("m_objWeb", BindingFlags.NonPublic | BindingFlags.Instance);

  88.             fi_m_objWeb.SetValue(profileImagePicker, web);

  89.             MethodInfo mi_LoadPictureLibraryInternal = profileImagePickerType.GetMethod("LoadPictureLibraryInternal", BindingFlags.NonPublic | BindingFlags.Instance);

  90.             if (mi_LoadPictureLibraryInternal != null)

  91.             {

  92.                 mi_LoadPictureLibraryInternal.Invoke(profileImagePicker, new object[] { });

  93.             }

  94.         }

  95.         public SPFile CreateThumbnail(Bitmap original, int idealWidth, int idealHeight, SPFolder folder, string fileName)

  96.         {

  97.             SPFile file = null;

  98.             // hack to get the Microsoft.Office.Server.UserProfiles assembly

  99.             Assembly userProfilesAssembly = typeof(UserProfile).Assembly;

  100.             // or assuming you know all the details of the assembly

  101.             // Assembly userProfilesAssembly = Assembly.Load(“Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”);

  102.             // UserProfilePhotos is internal class,

  103.             // so you cannot get it directly from Visual Studio

  104.             Type userProfilePhotosType = userProfilesAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfilePhotos");

  105.             MethodInfo mi_CreateThumbnail = userProfilePhotosType.GetMethod("CreateThumbnail", BindingFlags.NonPublic | BindingFlags.Static);

  106.             if (mi_CreateThumbnail != null)

  107.             {

  108.                 file = (SPFile)mi_CreateThumbnail.Invoke(null, new object[] { original, idealWidth, idealHeight, folder, fileName });

  109.             }

  110.             return file;

  111.         }

  112.         private SPFolder GetSubfolderForPictures(ProfileImagePicker profileImagePicker)

  113.         {

  114.             SPFolder folder = null;

  115.             Type profileImagePickerType = typeof(ProfileImagePicker);

  116.             MethodInfo mi_GetSubfolderForPictures = profileImagePickerType.GetMethod("GetSubfolderForPictures", BindingFlags.NonPublic | BindingFlags.Instance);

  117.             if (mi_GetSubfolderForPictures != null)

  118.             {

  119.                 folder = (SPFolder)mi_GetSubfolderForPictures.Invoke(profileImagePicker, new object[] { });

  120.             }

  121.             return folder;

  122.         }

  123.         private string GetFileNameFromAccountName(string accountName)

  124.         {

  125.             string result = accountName;

  126.             string charsToReplace = @"\/:*?""<>|";

  127.             Array.ForEach(charsToReplace.ToCharArray(), charToReplace => result = result.Replace(charToReplace, '_'));

  128.             return result;

  129.         }

  130.     }

  131. }


 

 

Happy profile image uploading!

You should know that user profile photos are stored in three different resolutions in a folder called Profile Pictures in the User Photos image library on the My Site Host site.

Note, that I found a bit strange dependency of behavior on the size of the photo you set in the PictureUrl profile property when it is about removing the profile image. It seems to be a minor issue, but might have significant impact on your solution.

When you upload the profile photo via the Edit User Profile user interface and then removes that also via the UI, then all of the image versions (I mean the three different resolution) are deleted. and using code. I consider this to be the standard behavior as it is not desired to leave orphaned files in the User Photos image library.

First I’ve tried to set the large image (_LThumb.jpg) from code in the PictureUrl profile property, but when I was to remove the image from the UI then only the large image was deleted, medium and small images remained in the image library. After checking the effect of setting the image from the UI I found, that it sets the PictureUrl property  to the medium image (_MThumb.jpg), so I modified the code accordingly. Now all of the images were removed as expected.

No comments:

Post a Comment