How to Import user photos in to MySites profile

I’ve come across several SharePoint installations where the HR department has used a Picture Library to store employee headshots.

The frequent request is how to get those images on to the user’s MySites profile so they appear in search results and permissions lists. This post will walk through the process I used to pull the Image url from the Pictures Library and update the user’s profile using Powershell.

Associate Picture Library record to a user name

The first thing that is needed is a way to identify the Photo according to the employee. The simplest way to do this is by adding a ‘Person or Group’ column to the Photo Library Content Type. This column will let you use the People Picker to select their domain account, which is exactly what is needed to retrieve their MySites profile record.

Add-Username-column

Powershell Script

The script is pretty straight forward.  Connect to the Picture Library, iterate through each entry, and for each one retrieve and update the User Profile record.  (The script below is adapted from what is found here.)

The trick comes mostly from extracting the Domain user name from the column we just added.

Add-PSSnapin Microsoft.SharePoint.Powershell

#Site URL
$mySiteUrl = "https://mysites.contoso.com/"
#The picture library site/web Url
$pictureLibUrl = "http://sara2.contoso.com/HumanResources/"
#The name of the picture library 
$pictureLibName = "Staff Photos"
#The internal name for PictureURL property on Profile
$profilePictureURLAttribute = "PictureURL"
#The internal name for Login property on Profile
$profileLoginAttribute = "AccountName"

#Get site objects and connect to User Profile Manager service
$site = Get-SPSite $mySiteUrl
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$profiles = $profileManager.GetEnumerator()

#Get web object and connect to Picture Library
$web = Get-SPWeb $pictureLibUrl
$gallery = $web.Lists[$pictureLibName]

write-host $gallery.ItemCount.ToString() "photo records found."

foreach ($photo in $gallery.Items ) {
	#get user assigment from column
	$fieldUser = [Microsoft.SharePoint.SPFieldUser]$photo.Fields.GetField('User Name')
	$fieldUserValue = [Microsoft.SharePoint.SPFieldUserValue]$fieldUser.GetFieldValue($photo['User Name'])

	if ($fieldUserValue) {
		$username = $fieldUserValue.User.UserLogin.ToString()
		if ($username) {
			#retrieve user's Mysites profile
			$profile = $profileManager.GetUserProfile($username)
			if ($profile) {
				#$photo.url will contain the doc lib path
				$newPictureUrl = $pictureLibUrl + $photo.Url
				$profile[$profilePictureUrlAttribute].Value = $newPictureUrl
				$profile.Commit()
				write-host "User: " $username "PictureUrl: " $newPictureUrl
			}
		}
	}
}

#clean up
$web.Dispose()
$site.Dispose()

#create thumbnails in Mysites
Update-SPProfilePhotoStore -MySiteHostLocation $mySiteUrl

There are two main things to point out. First is using the SPFieldUser and SPFieldUserValue objects to pull out the domain name from the User Name field.

#get user assigment from column
$fieldUser = [Microsoft.SharePoint.SPFieldUser]$photo.Fields.GetField('User Name')
$fieldUserValue = [Microsoft.SharePoint.SPFieldUserValue]$fieldUser.GetFieldValue($photo['User Name'])

Second is that after you have assigned the Url to the User Profile and saved it, you must run the Update-SPProfilePhotoStore cmdlet in order to properly create the necessary thumbnail images used by MySites.

#create thumbnails in Mysites
Update-SPProfilePhotoStore -MySiteHostLocation $mySiteUrl

Confirm Photo is on MySites Profile

After running the script, you can confirm the photo assignment by either visiting the user’s MySites profile page, or by editing the profile in Central Admininistration.


Posted

in

, ,

by

Tags:

Comments

6 responses to “How to Import user photos in to MySites profile”

  1. […] my last post, I talked about using a Powershell script to import user profile photos from a SharePoint Picture Library in to the MySites users […]

  2. Tiffany Avatar
    Tiffany

    Hi Derek,

    First off…Great stuff! I have a question. The column that you created “User Name”…is this field prefilled with the username when a picture is added to the Picture Library? I have a situation where I have a list of user photos that were exported from sql to to a network drive. The photos are in the name of employeeid.jpg (ex: 12345.jpg). I am trying to figure a way to correlate those photos with the user profile service so that I can update each user’s photo on their mysite profile page. Any suggesstions or sample would be of great help!

    1. Tiffany Avatar
      Tiffany

      I just saw your updated post using the workflow process and saw that the username field must be populated. So I guess I need a way to do this with the employee id field. I will upload the photos from the network drive to a picture library and will add the employeeid field. That’s my thoughts…however I need some guidance on the process (powershell or c# program). Thanks in advanced

      1. derek Avatar
        derek

        Tiffany – thanks for checking out the site.

        If you didn’t want to manually set the user name on each item in the picture library, you could definitely use a Powershell script to iterate through them and set the user name value. As long as you have a lookup to match the user name to the image filename, you should be good. A good example is at http://stackoverflow.com/questions/9946801/update-all-items-in-a-list-using-powershell

  3. Hayden Hancock Avatar
    Hayden Hancock

    I couldn’t get this to work. Yes, the picture get’s uploaded to the user’s profile but it’s at it’s original size. When I run the Update-SPProfilePhotoStore cmdlet, the additional images do not get created.

    I’ve attached a screenshot of what it looks like when change the value for the PictureUrl.

    http://imgur.com/ox5ATww

  4. Gabe Worden Avatar
    Gabe Worden

    I like you script, however we have a some people who don’t want a picture is it possible to rewrite it so you can have a default picture for many AD accounts?

Leave a Reply to Hayden Hancock Cancel reply