SharePoint 2010 : Upload Profile Pictures for all users

Consider the following scenarios:

-          You want pictures to be visible only in SharePoint Mysites or Site Collections. You ask users to provide pictures to a Shared Location along with their Employee name.

-          You don’t want users to have access to Mysites to change their Pictures .

-          You have not configured any Import/Export connection for Picture in User Profile Service Application.

-          You upgrade the Mysites content DB and not the Shared Service Provider (SSP) DB. Now, the profile pictures for the users are only updated if someone manually uploads it to their sites. The PictureURL property is set to NULL in profile DB. 

 The requirement is to have the pictures to be available only in SharePoint (no sync connection).

 

Resolution:

-          If the Mysites are on Claims Based Authentication 

-          Pictures to be stored by the account name i_0_.f_fbaprovider_accountname.jpg

  • Claims my site

Run the below mentioned Script to Upload the Picture to "User Photos" Gallery.

Make sure you change the FBA Provider and Location where you have stored the pictures

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("_")[1]

                $userName = $_.Name.Split("_")[4].Replace($_.Extension, "")

=====>   $adAccount ="i:0#.f|<your fba provider name as specified in central admin>" +$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()

    }

}

  • Run the below command after executing  the attached script

Upload-PhotosToSP -LocalPath "C:\Install\Photos" -MySiteUrl "https://portal/personal/MySite" -Overwrite

The command will upload the picture to the User Photos library in the my site as well as create the thumbnails.

 

  • NTLM my site

           Run the script mentioned below to Upload the Picture to "User Photos" Gallery.

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()
    }
}

       
Run the below command after executing the attached script

Upload-PhotosToSP -LocalPath "C:\Install\Photos" -MySiteUrl "https://portal/personal/MySite" -Overwrite

The command will upload the picture to the User Photos library in the my site as well as create the thumbnails.

 

Feel free to Post your comments if you have any questions or suggestions!