Upload User Profile Picture programmatically in SharePoint 2013


Hi,

We recently had a requirement to create a web part that would be used to update user profile picture and few other properties.

For this we used the FIileUpload control.

The code used


public void UploadPhoto(string accountName, byte[] image)
 {

string loginNameForProfile = accountName.Substring(accountName.IndexOf(@"|") + 1);

if (profileUpload.HasFile)
 {
 SPSecurity.RunWithElevatedPrivileges(delegate()
 {
 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
 {
 SPServiceContext serviceContext = SPServiceContext.GetContext(site);
 UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
 ProfilePropertyManager profilePropMgr =
 new UserProfileConfigManager(serviceContext).ProfilePropertyManager;

// Retrieve all properties for the "UserProfile" profile subtype,
 // and retrieve the property values for a specific user.
 ProfileSubtypePropertyManager subtypePropMgr =
 profilePropMgr.GetProfileSubtypeProperties("UserProfile");
 UserProfile userProfile = userProfileMgr.GetUserProfile(loginNameForProfile);

 SPSite mySite = new SPSite(userProfileMgr.MySiteHostUrl);
 SPWeb web = mySite.RootWeb;
 SPFolder subfolderForPictures = web.Folders["User Photos"].SubFolders["Profile Pictures"];

web.AllowUnsafeUpdates = true;

Stream fs = profileUpload.PostedFile.InputStream;

byte[] buffer = new byte[profileUpload.PostedFile.ContentLength];
 fs.Read(buffer, 0, Convert.ToInt32(profileUpload.PostedFile.ContentLength));
 fs.Close();
 int largeThumbnailSize = 300;
 int mediumThumbnailSize = 72;
 int smallThumbnailSize = 48;

 

string accName = accountName.Substring(accountName.IndexOf(@"\") + 1);
 using (MemoryStream stream = new MemoryStream(buffer))
 {
 using (Bitmap bitmap = new Bitmap(stream, true))
 {

 CreateThumbnail(bitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures,
 accName + "_LThumb.jpg");
 CreateThumbnail(bitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures,
 accName + "_MThumb.jpg");
 CreateThumbnail(bitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures,
 accName + "_SThumb.jpg");
 }
 }
 string pictureUrl = String.Format("{0}/{1}/{2}_MThumb.jpg", subfolderForPictures.ParentWeb.Site.Url, subfolderForPictures.Url, accName);
 // Change the value of a single-value user property.
 userProfile[PropertyConstants.PictureUrl].Value = pictureUrl;
 imgUserProfile.ImageUrl = pictureUrl;
 // Save the changes to the server.
 userProfile.Commit();

web.AllowUnsafeUpdates = false;
 }
 });

}
 }

public 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[] mi_methods = userProfilePhotosType.GetMethods(BindingFlags.NonPublic | BindingFlags.Static);

MethodInfo mi_CreateThumbnail = mi_methods[0];
 if (mi_CreateThumbnail != null)
 {
 file = (SPFile)mi_CreateThumbnail.Invoke(null, new object[] { original, idealWidth, idealHeight, folder, fileName, null });
 }

return file;
 } // end of function

The helpful post

http://lixuan0125.wordpress.com/2013/04/08/upload-user-profile-pictures-programmatically-sharepoint-2013/

Hope it helps

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

2 thoughts on “Upload User Profile Picture programmatically in SharePoint 2013”

  1. I read a lot of interesting content here. Probably you spend a lot of time
    writing, i know how to save you a lot of work, there is an online tool that creates readable,
    google friendly posts in minutes, just search in google – laranitas free
    content source

    Like

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM