12/21/2012
Date:
3:44:00 AM
Posted By
SharePoint
SharePoint User Profiles Service Application has a lot of nice and very useful features but it misses one small but important thing. Customers often want to display all their contacts (from User Profile Store) in a single list, phonebook style.
Here is a simple solution how to achieve this with a PowerShell script. The script queries the User Profile store and then copies user profile information to a simple SharePoint list (based on Contacts list template). The final result might look like this:
Please note:
- If your company has more than 1000 employees this solution might not scale properly.
- The script below was designed for English based SharePoint site templates, additional modifications might be required for other languages.
- The script must be executed under user that has both read privileges to User Profile Service Application and contribute privileges for your site.
Configuration steps:
- On your intranet/team site create a list based on the Contacts list template
- Create an additional column in this list: UserName – Single line of text
Here is the script:
02 | if(( Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell" }) -eq $null ) { |
03 | Add-PSSnapin Microsoft.SharePoint.PowerShell; |
05 | function Enumerate -SPUserProfiles ( $webUrl , $listName ) |
07 | $x = [System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.SharePoint" ) |
08 | $x = [System.Reflection.Assembly]::LoadWithPartialName( "microsoft.sharepoint.portal" ) |
09 | $x = [System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.Office.Server" ) |
10 | $x = [System.Reflection.Assembly]::LoadWithPartialName( "System.Web" ) |
12 | $context = [Microsoft.Office.Server.ServerContext]::GetContext( $sites [0]) |
13 | $profileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager( $context ) |
14 | $web = Get -SPWeb $webUrl |
15 | $list = $web .Lists[ $listName ] |
17 | $profileManager .GetEnumerator() | ?{$_.DisplayName -notlike '*\*' -and $_.ProfileType -eq "User" } | foreach-object { Update -PhoneBook $_ $web $list $webUrl } | |
19 | function Update -PhoneBook ( $user , $web , $list , $webUrl ) |
21 | if( $user .Item( "FirstName" ) -ne "" -and $user .Item( "FirstName" ) -ne $null ) |
23 | $spQuery = new-object Microsoft.SharePoint.SPQuery |
24 | $userName = $user .Item( "UserName" ) |
25 | $camlQuery = "<Where><Eq><FieldRef Name='UserName' /><Value Type='Text'>$userName</Value></Eq></Where>" ; |
26 | $spQuery .Query = $camlQuery |
28 | $spListItemCollection = $list .GetItems( $spQuery ) |
29 | $domain = ( gwmi WIN32_ComputerSystem).Domain |
30 | if( $spListItemCollection .Count -ge 1) |
32 | Write-Host "Updating $username..." |
33 | Update -Contact $spListItemCollection [0] $user $domain $webUrl |
37 | Write-Host "Adding $username..." |
38 | $contact = $list .items.add(); |
39 | Update -Contact $contact $user $domain $webUrl |
43 | function Update -Contact ( $contact , $user , $domain , $webUrl ) |
45 | $lastName = $user .Item( "LastName" ); |
46 | $contact [ "Title" ] = "$lastName" ; |
47 | $firstName = $user .Item( "FirstName" ); |
48 | $contact [ "FirstName" ] = "$firstName" ; |
49 | $workEmail = $user .Item( "WorkEmail" ); |
50 | $contact [ "Email" ] = "$workEmail" ; |
51 | $workPhone = $user .Item( "WorkPhone" ); |
52 | $contact [ "WorkPhone" ] = "$workPhone" ; |
53 | $CellPhone = $user .Item( "CellPhone" ); |
54 | $contact [ "CellPhone" ] = "$cellPhone" ; |
55 | $office = $user .Item( "Office" ); |
56 | $contact [ "Office" ] = "$office" ; |
57 | $department = $user .Item( "Department" ); |
58 | $contact [ "Department" ] = "$department" ; |
59 | $userName = $user .Item( "UserName" ); |
60 | $contact [ "UserName" ] = "$userName" ; |
You can run it by typing:
Tags