How to upload a user profile photo programmatically
Happy Profile Photo Uploading.......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Portal.WebControls;
using System.IO;
using Microsoft.Office.Server.UserProfiles;
namespace UploadProfileImage
{
class Program
{
static void Main(string[] args)
{
Program prog = new Program();
prog.UploadProfileImages();
}
private void UploadProfileImages()
{
String url = "http://SharePointSuresh/profiles/”
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
ProfileImagePicker profileImagePicker = new ProfileImagePicker();
InitializeProfileImagePicker(profileImagePicker, web);
SPFolder subfolderForPictures = GetSubfolderForPictures(profileImagePicker);
// repeat this block if you have more images and users
String accountName = @"domain\administrator";
// path to image file
String imageFilePath = @"C:\SharePointSuresh\Suresh.jpg";
UploadPhoto(accountName, imageFilePath, subfolderForPictures, profileImagePicker);
SetPictureUrl(accountName, subfolderForPictures);
// repeat block finished
}
}
}
private void SetPictureUrl(string accountName, SPFolder subfolderForPictures)
{
Console.WriteLine("Setting profile image for user '{0}'", accountName);
SPSite site = subfolderForPictures.ParentWeb.Site;
UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.GetContext(site));
UserProfile userProfile = userProfileManager.GetUserProfile(accountName);
string fileNameWithoutExtension = GetFileNameFromAccountName(accountName);
string pictureUrl = String.Format("{0}/{1}/{2}_MThumb.jpg", site.Url, subfolderForPictures.Url, fileNameWithoutExtension);
userProfile["PictureUrl"].Value = pictureUrl;
userProfile.Commit();
}
private void UploadPhoto(string accountName, string imageFilePath, SPFolder subfolderForPictures, ProfileImagePicker profileImagePicker)
{
Console.WriteLine("Uploading image '{0}' for user '{1}'", imageFilePath, accountName);
if (!File.Exists(imageFilePath) || Path.GetExtension(imageFilePath).Equals(".gif"))
{
Console.WriteLine("File '{0}' does not exist or has invalid extension", imageFilePath);
}
else
{
string fileNameWithoutExtension = GetFileNameFromAccountName(accountName);
FileStream file = File.Open(imageFilePath, FileMode.Open);
BinaryReader reader = new BinaryReader(file);
if (subfolderForPictures != null)
{
// try casting length (long) to int
byte[] buffer = reader.ReadBytes((int)file.Length);
int largeThumbnailSize = 0×90;
int mediumThumbnailSize = 0×60;
int smallThumbnailSize = 0×20;
using (MemoryStream stream = new MemoryStream(buffer))
{
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");
}
}
}
}
}
private void InitializeProfileImagePicker(ProfileImagePicker profileImagePicker, SPWeb web)
{
Type profileImagePickerType = typeof(ProfileImagePicker);
FieldInfo fi_m_objWeb = profileImagePickerType.GetField("m_objWeb", BindingFlags.NonPublic | BindingFlags.Instance);
fi_m_objWeb.SetValue(profileImagePicker, web);
MethodInfo mi_LoadPictureLibraryInternal = profileImagePickerType.GetMethod("LoadPictureLibraryInternal", BindingFlags.NonPublic | BindingFlags.Instance);
if (mi_LoadPictureLibraryInternal != null)
{
mi_LoadPictureLibraryInternal.Invoke(profileImagePicker, new object[] { });
}
}
public SPFile CreateThumbnail(Bitmap original, int idealWidth, int idealHeight, SPFolder folder, string fileName)
{
SPFile file = null;
// hack to get the Microsoft.Office.Server.UserProfiles assembly
Assembly userProfilesAssembly = typeof(UserProfile).Assembly;
// or assuming you know all the details of the assembly
// Assembly userProfilesAssembly = Assembly.Load(“Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”);
// UserProfilePhotos is internal class,
// so you cannot get it directly from Visual Studio
Type userProfilePhotosType = userProfilesAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfilePhotos");
MethodInfo mi_CreateThumbnail = userProfilePhotosType.GetMethod("CreateThumbnail", BindingFlags.NonPublic | BindingFlags.Static);
if (mi_CreateThumbnail != null)
{
file = (SPFile)mi_CreateThumbnail.Invoke(null, new object[] { original, idealWidth, idealHeight, folder, fileName });
}
return file;
}
private SPFolder GetSubfolderForPictures(ProfileImagePicker profileImagePicker)
{
SPFolder folder = null;
Type profileImagePickerType = typeof(ProfileImagePicker);
MethodInfo mi_GetSubfolderForPictures = profileImagePickerType.GetMethod("GetSubfolderForPictures", BindingFlags.NonPublic | BindingFlags.Instance);
if (mi_GetSubfolderForPictures != null)
{
folder = (SPFolder)mi_GetSubfolderForPictures.Invoke(profileImagePicker, new object[] { });
}
return folder;
}
private string GetFileNameFromAccountName(string accountName)
{
string result = accountName;
string charsToReplace = @"\/:*?""<>|";
Array.ForEach(charsToReplace.ToCharArray(), charToReplace => result = result.Replace(charToReplace, '_'));
return result;
}
}
}
Comments
Post a Comment