Tuesday, 20 July 2010

Modify multi value user profile properties in SharePoint with PowerShell

Following on from my previous article on how to modify single value user profile properties in SharePoint 2010 using PowerShell, this script shows how you can modify multi value properties, which are stored in the Keywords term set in the Managed Metadata service application. There is actually not a lot of difference between the two scripts – the only change being the part of the script that modifies the property. The key difference is that this script adds a term to the list of terms already present in the property rather than replacing the value as before.

The example I have used here is to add the term “SharePoint” to the list of skills for the user DOMAIN\phil.childs. If the term is already present in the Keywords term set, then it will retrieve it and add it to the user profile property. If it is not currently in the term set, then it will add it for you before populating the user profile property:

#Set up default variables
$mySiteUrl = "http://mysite"
$adAccount = "DOMAIN\phil.childs"
$upAttribute = "SPS-Skills"
$upAttributeValue = "SharePoint"

#Get site objects and connect to User Profile Manager service
$site = Get-SPSite $mySiteUrl
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

#Check to see if user profile exists
if ($profileManager.UserExists($adAccount))
{
    #Get user profile
    $up = $profileManager.GetUserProfile($adAccount)
    $up[$upAttribute].Add($upAttributeValue)
    $up.Commit()
}
else
{
    write-host "Profile for user"$adAccount "cannot be found"
}

#Dispose of site object
$site.Dispose()

A few things to note with this script:

  • Make sure you have the correct permissions configured on the User Profile Service Application (see previous article for details)
  • As before, you will need to specify the My Site host site collection URL for the $mySiteUrl variable
  • You have to use the internal name of the user profile property when assigning the $upAttribute variable – e.g., “SPS-Skills” for the Skills property – this is also the same for single value properties, too (forgot to mention this in my previous article). You can find the internal name of a property by going to the Manage User Properties page of the User Profile Service Application and editing a property

Manage User Properties 

Along with adding terms to a multi value user profile property, you may also want to remove them. You can do this by using the same script above and replacing the $up[$upAttribute].Add($upAttributeValue) line with the following, which cycles through all the terms assigned to the user profile property and removes the term you have specified once found:

for ( [int]$i = 0; $i -lt $up[$upAttribute].Count; $i++ )
{
    if ($up[$upAttribute][$i] -eq $upAttributeValue) { $up[$upAttribute].RemoveAt($i) }
}

Finally, if you want to remove ALL terms from the user profile property (e.g., delete all terms from the Skills property for that user), then replace the $up[$upAttribute].Add($upAttributeValue) line with the following:

$up[$upAttribute].Clear()

12 comments:

  1. Thanks for the post.

    I used some of it as inspiration.

    One note: The line
    $up[$upAttribute].Add($upAttributeValue)
    Will fail if you execute it twice (i.e. for an existing term set value).

    ReplyDelete
  2. A crap, my bad.

    My comment above is only valid if you have bound the multi-valued field to a term set.

    ReplyDelete
  3. Thanks for this it's really useful. One thing that I am struggling with is being able to add multiple values into the multi-value field - e.g. someone has more than one skill (e.g. SharePoint 2007 and SharePoint 2010" how do you pass both through to the UPS via Powershell?

    ReplyDelete
  4. $myStringFromCSV = "SharePoint 2007; SharePoint 2010; SharePoint 2013"

    $values = $myStringFromCSV.Split(";")

    foreach ($value in $values)
    {
    $up["SPS-Responsibility"].Add($value)
    }

    $up.Commit()

    ReplyDelete
  5. Thank you - finally got a step in the right way :-)

    If the script should loop through all the users MySites and delete e.g. 3 tags - how should the script be extended? (I'm a NOOB to powershell)

    ReplyDelete