Tuesday, 23 September 2014

Import User Pictures from Active Directory to SharePoint 2010 My Site

My Organization decided to use Active Directory as a central repository for user profile photos to share with Lync 2010, Outlook and of course SharePoint!

Lets keep other products aside and talk about How to Import User Pictures from Active Directory to SharePoint 2010 My Site. Here is the default My site Profile page without Profile photo imported from AD:

import user pictures from active directory to sharepoint 2010

Well, this article addresses import user pictures from active directory to SharePoint 2010 step by step:
  1. Import user photo into Active Directory user profile's "thumbnailPhoto" attribute.
  2. Configure Property mapping between SharePoint and Active Directory and Run FULL Profile Import.
  3. Run Update-SPProfilePhotoStore cmdlet to generate thumbnails for SharePoint user profile picture.
Assuming UPS is in place, My Site is up and running, SharePoint 2010 SP1 is installed (or October 2010 Cumulative Update), Lets get into details.

Step 1: Import user photo into Active Directory user profile's "thumbnailPhoto" attribute.
If profile pictures are not already in Active Directory user profile's "thumbnailPhoto" attribute, We can import pictures into it. For SharePoint 2010 to import user photo from ad, it must have this attribute populated.
import photo from ad sharepoint 2010

Yes, there are 3rd party tools like AD Photo Edit to import user profile thumbnail photos in bulk,  PowerShell can do it well with just three lines!
?
1
2
3
4
5
Import-Module ActiveDirectory
 
$photo=[byte[]](Get-Content .\salaudeen_photo.jpg -Encoding byte)
 
Set-ADUser "Salaudeen" -Replace @{thumbnailPhoto=$photo}

PowerShell Script to Bulk Import User Profile Photos into AD:
Lets say, we've lot of users in AD and their user name, Profile Photo file names are stored in a CSV file with columns "UserName", "PhotoFileName". We can utilize PowerShell to import photo to AD in bulk.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#Read the CSV file - Map the Columns to Named Header (CSV File doesn't has Column Header)
$CSVData = Import-CSV -path "C:\Scripts\UpdateProfilePhoto\UserPhotos.csv" -Header("UserName", "PhotoFileName")
 
 #Iterate through each Row in the CSV
 foreach ($row in $CSVData)
   {
        #Get the User Name & Photos from CSV file
        $userName= $row.UserName
        $PhotoFile = Join-path "C:\Scripts\UpdateProfilePhoto" $row.PhotoFileName
         
        #Get the User from AD
        $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
        $root = $domain.GetDirectoryEntry()
        $search = [System.DirectoryServices.DirectorySearcher]$root
        $search.Filter = "(&(objectCategory=User)(samAccountName=$userName))"
        $result = $search.FindOne()
          
        if ($result -ne $null)
        {
            #Get the User Object
            $user = $result.GetDirectoryEntry()
             
            #Get the Photo from Disk
            $photo=[byte[]](Get-Content $PhotoFile -Encoding byte)
            
            #Update User Profile Photo
            $user.put("thumbnailPhoto"$photo )
            $user.setinfo()
             
            Write-Host $user.displayname " Photo has been updated."
        }
         
    }
Once imported, You can find the "thumbnailPhoto" property updated in AD User's properties.
import pictures from active directory to sharepoint 2010
BTW, User profile picture can be from any where! Not just AD, but any File Server, SharePoint Library, etc. So you can proceed by setting up a ProfileURL mapping and execute below steps.

Step 2: Configure Property mapping between SharePoint and Active Directory and Run FULL Profile Import.
Once user profile pictures are imported to Active directory, the next step is to make SharePoint aware of the Property by creating a Property Mapping. Go to:
  • Central Administration >> Manager Service Application
  • Click on "Manage" on User Profile Service Application
  • Click on "Manage User Properties"
  • Edit the "Picture" property
    import user profile photos from active directory into sharepoint 2010
  • We don't want users to override the photo from AD. So select "Do not allow users to edit value for this property" option
    import ad photos into sharepoint 2010
  • Specify the User Profile Connection, Attribute as "thumbnailPhoto" and direction as "Import"
    sharepoint 2010 import picture from active directory
  • click "Add" button and then "OK".
So, we've done mapping the AD property thumbnailPhoto with SharePoint via “PictureURL” User Profile property.

Trigger FULL User Profile Synchronization 
Since UPS didn’t know about this field previously, We've to Run FULL user profile sync. From User Profile Service Application, click on "Start Profile Synchronization" >> Select "Start Full" >> Click "OK" button.
 import pictures from active directory to sharepoint
wait for the user profile Synchronization to complete, and then proceed to next step. So now, we've import user profile photos from active directory into SharePoint 2010.

Step 3: Run Update-SPProfilePhotoStore cmdlet to generate thumbnails for SharePoint user profile picture.
SharePoint 2010 requires the thumbnails to be within 10kb size of 32x32, 96x96, 144x144 px resolutions. So, We've to instruct SharePoint to generate the image thumbnails and re-configure user profiles to reference the thumbnail.

PowerShell Script to create thumbnails in the right sizes and maps the images to the user profiles:
?
1
2
3
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
Update-SPProfilePhotoStore -CreateThumbnailsForImportedPhotos 1 -MySiteHostLocation  http://<My Site Host Web App URL>
Make sure You have Administrator access and "FULL" control Permissions on User Profile Service Application.  Otherwise, You will get "Update-SPProfilePhotoStore: Object reference not set to an instance of an object." Error!
sharepoint 2010 import photo from active directory
In some cases, It’s also important to note that you must be a site collection administrator of the ‘My Site Host’, and also have administrative access to the SQL Databases. On successful execution, SharePoint 2010 Stores user profile pictures under My site Host's, "User Photos" library. E.g. http://mysite.crescent.com/User%20Photos

Verify the Result: Navigate to My site Personal profile (person.aspx) page and verify the new photo. Same Procedure applies to SharePoint 2013 also.
sharepoint 2010 import user photo from ad
Remember, SharePoint search will still show you the old photo until the next incremental search crawl takes place on the content source with your sps3:// provider mapped!

On Going Maintenance: Once AD Profile photo is updated down the line, SharePoint 2010 user profile synchronization will pick the photo. But still we've to make the photo ready for SharePoint by executing Update-SPProfilePhotoStore cmdlet on schedule basis.

So, Its a good idea to schedule a script in Windows Task scheduler to run Update-spprofilephotostore cmdlet after completing FULL profile import. Say, You have the script in a file "SPProfilePhotoStore.ps1" under "D:\Scripts".  Schedule a PowerShell script in Windows task Scheduler and enter "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe "& 'D:\Scripts\Update-SPProfilePhotoStore.ps1'" in Program to execute.

 or in arguments box, you can directly enter: -NonInteractive -NoProfile -Command "& {Add-PSSnapin Microsoft.SharePoint.PowerShell;Update-SPProfilePhotoStore -MySiteHostLocation https://<My site Host URL> -CreateThumbnailsForImportedPhotos 1}"



Reference:


No comments:

Post a Comment