Import User Photos

Post date: Aug 23, 2013 8:13:17 PM

As part of a migration project to implement SharePoint 2013, I needed a method to import all the user photos from our existing server. I had found a script at http://get-spscripts.com/2010_12_01_archive.html which provided most of the legwork I was after but did need to modify it a bit as the user photo file names did not include a domain; however, I did know for a fact that every user was under one of two domains; furthermore, I also knew for a fact that there were no two users who shared the same username but were in different domains.

The following line executes this stored function using Powershell:

Upload-PhotosToSP -LocalPath "E:\Install\Photos" -MySiteUrl "http://mysites" -Overwrite

The following is the function with my modifications:

function Upload-PhotosToSP

{

Param (

[parameter(Mandatory=$true)][string]$LocalPath,

[parameter(Mandatory=$true)][string]$MySiteUrl,

[parameter(Mandatory=$false)][switch]$Overwrite

)

#Get site, web and profile manager objects

$mySiteHostSite = Get-SPSite $MySiteUrl

$mySiteHostWeb = $mySiteHostSite.OpenWeb()

$context = Get-SPServiceContext $mySiteHostSite

$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

try

{

#Get files from local folder

$localPhotosFolder = Get-ChildItem $LocalPath

#Get User Photos document library in the My Site host site

$spPhotosFolder = $mySiteHostWeb.GetFolder("User Photos")

#Upload each image file and configure user profiles

$localPhotosFolder | ForEach-Object {

#Generate file path for upload into SharePoint

$spFullPath = $spPhotosFolder.Url + "/" + $_.Name

#Check if the file exists and the overwrite option is selected before adding the file

if ((!$mySiteHostWeb.GetFile($spFullPath).Exists) -or ($Overwrite)) {

#Add file to the User Photos library

write-host "Copying" $_.Name "to" $spFullPath.Replace("/" + $_.Name,"") "in" $mySiteHostWeb.Title "..." -foregroundcolor Green

$spFile = $spPhotosFolder.Files.Add($spFullPath, $_.OpenRead(), $true)

$spImagePath = $mySiteHostWeb.Url + "/" + $spFile.Url

#Get the domain and user name from the image file name

$domainName = "VADOM"

$userName = $_.Name.Replace($_.Extension, "")

$adAccount = $domainName + "\" + $userName

#Check to see if user profile exists

if ($profileManager.UserExists($adAccount))

{

#Get user profile and change the Picture URL value

$up = $profileManager.GetUserProfile($adAccount)

$up["PictureURL"].Value = $spImagePath

$up.Commit()

}

#If the VADOM user account does not exist, try CORP

else

{

$domainName = "CORP"

$adAccount = $domainName + "\" + $userName

if ($profileManager.UserExists($adAccount))

{

#Get user profile and change the Picture URL value

$up = $profileManager.GetUserProfile($adAccount)

$up["PictureURL"].Value = $spImagePath

$up.Commit()

}

else

{

write-host "Profile for user"$userName "cannot be found"

}

}

}

else

{

write-host "`nFile"$_.Name "already exists in" $spFullPath.Replace("/" + $_.Name,"") "and shall not be uploaded" -foregroundcolor Red

}

}

#Run the Update-SPProfilePhotoStore cmdlet to create image thumbnails and update user profiles

write-host "Waiting to update profile photo store - Please wait..."

Start-Sleep -s 60

Update-SPProfilePhotoStore -MySiteHostLocation $MySiteUrl

write-host "Profile photo store update run - please check thumbnails are present in Profile Pictures folder."

}

catch

{

write-host "The script has stopped because there has been an error: "$_

}

finally

{

#Dispose of site and web objects

$mySiteHostWeb.Dispose()

$mySiteHostSite.Dispose()

}

}