One of my favorite features of SharePoint 2010 is the User Profile. I think it is one of the most under utilized yet great value enhancement since the last version. Many companies struggle with Identity Management and a single directory for accurate information lookup. With the User Profile we can solve this problem. Although the User Profile Service Synchronization may be a little bit of a pain to initially get setup and configured as documented in this previous post: http://sharepoint.licomputersource.com/2010/07/23/installing-and-configuring-user-profile-synchronization-service-in-sharepoint-2010/

Once running it is a very valuable asset and tool for a company, especially large global organizations. With the ability to sync from multiple sources it provides the ever so desired ability of a single source for all user information. Then add in user sub-types and organization profiles and sub-types and we have a very nice robust platform to find users, use as a global phone directory and more.

One thing that I see fairly regularly is that the profile pictures are not there for the organizations that are using the user profile. This is a shame especially in large organizations. I know the picture isn’t needed but it is nice to have a face to the person that you deal with all of the time yet never get to see. Most companies have the pictures for each user already as they assign a badge with photo on it. So why don’t we use that? Well mainly its one of two things, we don’t think about it or we don’t know how to pull the pictures out of the badge system and put them into SharePoint. Today I will go through how to programmatically upload and display pictures for each user. We will use a csv file that has some fields on it and it will upload, assign, and convert the picture for a number of users. We won’t discuss the badge system piece as that could be different for everybody. For my system there was a database of users and information along with photo number. The photos were stored in a file share.

To accomplish this task I did use the SPUtils.ps1 functions which can be found here. Thanks to Chris Johnson for his work on this. Today we will cover some of the same stuff found in his post, but add on to allow you to do multiple users at the same time. Chris covers it on an individual basis.

I also used the Quest Active Directory Commandlets found here

You will want to load the Quest Active Roles snappin and the functions in SPUtils.ps1 before trying to run anything else below.

So to get started I took all of the photos from the file share and uploaded them to a SharePoint picture library. You can do this a number of different ways, use explorer view and copy and paste, or use the UploadSPFile PowerShell commandlet.

So now we have a library full of user pictures. We take a csv file that has whatever fields you want really but at least the picture and loginName columns.

So now for the real work. We now use PowerShell to import this csv file and go through and set the profile property for each user. Here is the script, items that must be changed are highlighted in green. This script also assumes that the column headings in your csv file are “LoginName” and “Picture”

function Set-UserPictures([string] $UserFile)
{
    $siteUrl = http://<mysitehosturl>
    $upm = GetProfileManager -url $siteUrl
    $picturePropertyName = GetProfilePropertyName -UserProfileManager $upm -PropertyName "PictureUrl";
    Import-Csv $UserFile | foreach-object {
                $User = Get-QADUser $_.LoginName
                If($User){
                    $name = "DOMAIN" + $_.LoginName;
                    $up = $upm.GetUserProfile($name);
                    $fullURL = http://<url to where you uploaded the pictures in previous step>/ + $_.Picture;
                    $up.get_Item($picturePropertyName).Value = $fullURL
                    $up.commit()
                    Write-Host "Updated Picture for $name" -ForegroundColor Blue
                }else{
                    Write-Host $_.LoginName "Not Found" -ForegroundColor Green
}
                    }
}

The PS1 file itself can be downloaded here.

After you have loaded the function you can run it by typing:

Set-UserPictures c:<path to csv file>

This sets the Profile Picture however if you look at your profile page now you see there is a broken image. This is expected. If you don’t have a broken image something went wrong.

After the script completes you should then run

Update-SPProfilePhotoStore –MySiteHostLocation “http://<my site host url>/”

This command is great. What it does is it takes a look at every profile picture and resizes it into 3 different sizes. The three different sizes that SharePoint natively uses for these pictures, such as thumbnail and it saves it in SharePoint. Then it goes and resets the profile picture to the appropriately sized one. After this completes, you should see your photos displaying nicely.