If you have used the Central Administration to add the User Profile Properties you are aware that it can be frustrating with the wait time on adding a single property. That led me to a research on how can we add User Profile Properties through PowerShell. That landed me to this blog – http://gallery.technet.microsoft.com/scriptcenter/SP2010-PowerShell-to-0fda04f7
It worked for me for single property but most of our clients do have multiple properties and that does come from separate connections including External Content Types. So, I did modify the above script and created a csv file of all the available properties that I needed to track. The CSV file has the following properties –
- PropertyName – This field is used for the Internal Name of the User Profile.
- DisplayName – This field is used for the Display Name property of the User Profile Property.
- Privacy – The allowed values are – Public, Contacts, Organization, Manager, Private and NotSet
- Privacy Policy – The allowed values are – Mandatory, OptIn, OptOut and Disabled
- Type – The data type of the property.
- Length – In case of string, enter this value or enter 0
- ConnectionName – The Synchronization connection name.
- AttributeName – The attribute from the synchronization that you would like to map to.
- IsMultivalued – Whether this property will hold multiple values.
My CSV file example looks as –
PropertyName,DisplayName,Privacy,PrivacyPolicy,Type,Length,ConnectionName,AttributeName,IsMultivalued
testskills,Test Skills,Public,Disabled,string,200,EmployeeSkills,Skill,true
testskills1,Test Skills1,Public,Disabled,string,200,EmployeeSkills,Skill,true
EmployeeSkills is the BCS connection that pulls in skills from our database.
The PowerShell script mentioned in the above blog was not working for me. So, I made a few changes. This script accepts two parameters –
Parameter 1 – The csv file path and name
Parameter 2 – The Site URL.
The main thing I added was the IsMultivalued property and also the check to see if the type is string then assign the length. The entire selection is within a foreach loop that goes through each line to pull the property information.
param($Parm1, $Parm2)
#Load SharePoint User Profile assemblies
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server”)
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server.UserProfiles”)
$userProps = Import-Csv $Parm1
#Get UserProfileManager
$mySiteUrl = $Parm2
$site = Get-SPSite $mySiteUrl
$context = Get-SPServiceContext $site
$upConfigManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($context)
$profilePropertyManager = $upConfigManager.ProfilePropertyManager
$coreprofilePropertyManager = $profilePropertyManager.GetCoreProperties()
$userProfileTypeProperties = $profilePropertyManager.GetProfileTypeProperties([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$userProfileSubTypeManager = [Microsoft.Office.Server.UserProfiles.ProfileSubTypeManager]::Get($context)
$userProfile = $userProfileSubTypeManager.GetProfileSubtype([Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName([Microsoft.Office.Server.UserProfiles.ProfileType]::User))
$userProfileProperties = $userProfile.PropertiesForeach ($userProp in $userProps)
{
#Set Custom Property values
$PropertyName = $userProp.PropertyName
$PropertyDisplayName = $userProp.DisplayName
$Privacy= $userProp.Privacy
$PrivacyPolicy = $userProp.PrivacyPolicy
$coreProperty = $coreprofilePropertyManager.Create($false)
$coreProperty.Name = $PropertyName
$coreProperty.DisplayName = $PropertyDisplayName
$coreProperty.Type = $userProp.Type#Check if the Type is string then assign the length.
if ($userProp.Type -eq ‘string’)
{
$coreProperty.Length = $userProp.Length
}if ($userProp.IsMultivalued -eq ‘true’)
{
$coreProperty.IsMultivalued = $true
}
$foundProperty = $coreprofilePropertyManager.GetPropertyByName($userProp.PropertyName)#if the property is found then we delete that property.
if ($foundProperty -ne $null)
{#Uncomment the code to delete the property.
#$coreprofilePropertyManager.RemovePropertyByName($userProp.PropertyName)
}
$coreprofilePropertyManager.Add($coreProperty)
$profileTypeProperty = $userProfileTypeProperties.Create($coreProperty)
#Show on the Edit Details page
$profileTypeProperty.IsVisibleOnEditor = $true
#Show in the profile properties section of the user’s profile page
$profileTypeProperty.IsVisibleOnViewer = $true
#Show updates to the property in newsfeed
$profileTypeProperty.IsEventLog = $true
$userProfileTypeProperties.Add($profileTypeProperty)
$profileSubTypeProperty = $userProfileProperties.Create($profileTypeProperty)
$profileSubTypeProperty.DefaultPrivacy =[Microsoft.Office.Server.UserProfiles.Privacy]::$Privacy
$profileSubTypeProperty.PrivacyPolicy = [Microsoft.Office.Server.UserProfiles.PrivacyPolicy]::$PrivacyPolicy
$userProfileProperties.Add($profileSubTypeProperty)
#Add New Mapping for synchronization user profile data
#SharePoint Synchronization connection
$connectionName =$userProp.ConnectionName
#Attribute name in Connection Source$attributeName =$userProp.AttributeName
$synchConnection = $upConfigManager.ConnectionManager[$connectionName]
$synchConnection.PropertyMapping.AddNewMapping([Microsoft.Office.Server.UserProfiles.ProfileType]::User,$PropertyName,$attributeName)}
You can use this code at your own risk 🙂
Good luck with PowerShell Scripting.
ngsrupesh
June 8, 2016 at 2:48 pm
I Tried this code:
[String]$accountName=”corp\abc”
[String] $colleagueAccountName=”corp\xyz”
[String] $group=”General”
$privacy=”Public”
$isInWorkGroup=$true
$upws = New-WebServiceProxy “http://corp:11111/sites/demo/_vti_bin/UserProfileService.asmx?wsdl” -UseDefaultCredential
$contactData=$upws.AddMembership($accountName,$colleagueAccountName,$group,$privacy,$isInWorkGroup)
Write-Host -ForegroundColor Green “New Colleague added successfully”
$contactData
Not working