2014-11-20

I was looking for a scripted way of creating a Userprofile Multivalue managed metadata based property and to set value against it. The below sample script creates a managed metadata based user profile property and sets a multivalue term against it.

Create a managed metadata user profile property: 

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
[System.Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[System.Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
[System.Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles.UserProfileManager")

$siteUrl = "<<site url>>"
$propertyName = "UPTaxProperty"
$termSetName = "MyUPTermSet"

$site = Get-SPSite $siteUrl
$serviceContext = Get-SPServiceContext $site

$upConfigMgr = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($serviceContext)

$coreProperties = $upConfigMgr.ProfilePropertyManager.GetCoreProperties()

if($coreProperties.GetPropertyByName("UPTaxProperty") -ne $null)
{
$coreProperties.RemovePropertyByName("UPTaxProperty")
}

$upcoreproperty = $coreProp.Create($false)
$upcoreproperty.Name = $propertyName
$upcoreproperty.DisplayName = $propertyName
$upcoreproperty.Type = [Microsoft.Office.Server.UserProfiles.PropertyDataType]::StringMultiValue
$upcoreproperty.IsMultivalued = $true #creating a multi value taxonomy property

$taxonomySession = Get-SPTaxonomySession -Site $site
$mytermSet = $taxonomySession.GetTermSets($termSetName,1033)
$upcoreproperty.TermSet = $mytermSet[0]
$coreProperties.Add($upcoreproperty)

$userProfilePropertyManager = $upConfigMgr.ProfilePropertyManager
$userProfileTypeProperties = $userProfilePropertyManager.GetProfileTypeProperties[Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$userProfileProperty = $userProfileTypeProperties.Create($upcoreproperty)
$userProfileProperty.IsVisibleOnEditor = $true
$userProfileProperty.IsVisibleOnViewer = $true
$userProfileTypeProperties.Add($userProfileProperty)

$upSubTypeManager = [Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::Get($serviceContext)
$userProfile = $upSubTypeManager.GetProfileSubtype[Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName[Microsoft.Office.Server.UserProfiles.ProfileType]::User))
$userProfileProperties = $userProfile.Properties
$userProfileSubProperty = $userProfileProperties.Create($userProfileProperty)
$userProfileSubProperty.DefaultPrivacy = [Microsoft.Office.Server.UserProfiles.Privacy]::Public
$userProfileSubProperty.PrivacyPolicy = [Microsoft.Office.Server.UserProfiles.PrivacyPolicy]::OptIn
$userProfileSubProperty.IsUserEditable = $true
$userProfileSubProperty.UserOverridePrivacy = $true

$userProfileProperties.Add($userProfileSubProperty)

$userProfileProperty.CoreProperty.Commit()
$userProfileProperty.Commit()
$userProfileSubProperty.Commit()

Screenshot:

Set Managed metadata user profile value:

[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server");
$site=get-spsite "<<site url>>"
$serviceContext = Get-SPServiceContext $site
$userprofilemgr = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
$username = "<<username>>"
if($userprofilemgr.UserExists($username)){
$userProfile = $userprofilemgr.GetUserProfile($username);
$userProfile["UPTaxProperty"].value = @("Term1","ChildTerm1")
$userProfile.Commit()
}

Screen Shot:


About the author 

Balamurugan Kailasam