SharePoint: Upload Photos to MySites

Since we do not use MySites in SharePoint in the traditional sense, we needed to find a way to programmatically upload photos of our Users to their MySites.  This would allow us to populate their information and photo to use the site as more of an employee phone directory.

To accomplish this, I created a folder on the C Drive of our SharePoint Web Front End called SPPhotoUpload.  It contained the following folders:

  • Photos
  • Uploaded Photos

Next, I created a PowerShell Script file called PhotoUpload.PS1:


Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

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 = $_.Name.Split(“_”)[0]
$userName = $_.Name.Split(“_”)[1].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()
}
else
{
write-host “Profile for user”$adAccount “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()
}
}

Upload-PhotosToSP -LocalPath “C:\SPPhotoUpload\Photos\” -MySiteUrl “http://mysites.domain.com” -Overwrite

The last line of this script should be modified with your own local path and MySite URL.

This script will take a photo of any size and create 3 new files to upload to the user’s profile.

To use the script:

1) Add photos to the Photo folder with the naming convention domain_username.jpg

2) Open a Powershell Window using the SharePoint Farm Administrator credentials:

3) Run the following command:

powershell.exe C:\SPPhotoUpload\PhotoUpload.PS1

4) Once you are done, you should move the photos from the Photo folder to Uploaded Photos to use for later.

I wish I could take full credit for this script because it works perfectly, so a big thanks is in order for this post I found:

http://get-spscripts.com/2010/12/upload-multiple-user-profile.html

Advertisements
This entry was posted in SharePoint 2010 and tagged , , . Bookmark the permalink.

One Response to SharePoint: Upload Photos to MySites

  1. http://gettodayinsurance.org/ – progressive car insurance
    cheap auto insurance
    cheap auto insurance

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