Get the User’s Profile Image in SharePoint 2010

 

First, some details.

SharePoint allows users (by default) to upload a photo for their profile. When they do so, SharePoint automatically creates three different sized versions of the image, and stores them in the following location:

http://<mysitehost>/User Photos/Profile Pictures

If you’re not sure what the “mysitehost” path is, just open your SharePoint site and go to your profile page. On my dev system, that URL is:

http://localhost/my/Person.aspx?accountname=….

So, in the above, the mysitehost portion is http://localhost/my. To get to the profile images, I can navigate to: http://localhost/my/User Photos/Profile Pictures

The names of the images are in the following pattern:

  • Domain_username_LThumb
  • Domain_username_MThumb
  • Domain_username_SThumb

L, M, and S stand for 144×144, 96×96, and 42×42, respectively.

At least, the images should be there. One of the Post-upgrade steps for migrating from SharePoint 2007 is running a PowerShell command to upgrade the photo store. If that was not done, then the photos will not be in this location…

(More details on the photos are here: And, info about upgrading the photo store for an in place upgrade is here.)

 

So, on to getting a user’s photo url programmatically:

 

public static string GetUserProfilePictureURL(string username)
{
    string Url = string.Empty;
            
    SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
    UserProfileManager upm = new UserProfileManager(serviceContext);
    if (upm.UserExists(username))
    {
        UserProfile profile = upm.GetUserProfile(username);
        if (profile != null)
        {
            Url = profile["PictureURL"].Value.ToString();
        }
    }

    //change it to the default if the user does not have an image specified
    if (string.IsNullOrEmpty(Url))
        Url = "/_layouts/images/O14_person_placeHolder_96.png";

    return Url;
}

 

Note that the “PictureURL” property stores the value of the medium photo. There is no API that I’m aware of to request the small or large image.

Advertisements

2 thoughts on “Get the User’s Profile Image in SharePoint 2010

  1. ismapro

    Thanks it really helps me… the only thing is that doesn’t work on SharePoint Foundation because the User Profile Service Application is not present on it. But in standart and enterprise works perfectly

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s