Updating SharePoint User Profile Property with a Taxonomy Term

I'm experiencing a problem trying to update a SharePoint User Profile property that is linked to a taxonomy TermSet.  The following code results in an exception.

$spsite = Get-SPSite $mysiteurl
$context = Get-SPServiceContext $mysiteurl
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

if ($upm.UserExists($adAccount))
{
    $user = $upm.GetUserProfile($adAccount)
    $locationsCollection = $user[$propName]

    $taxMgr = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($spsite)
    $taxStore = $taxMgr.TermStores[0]
    $officeLocationsGroup = $taxStore.Groups["Office Locations"]
    $officeLocationsTermSet = $officeLocationsGroup.TermSets["Office Locations"]

    $terms = $officeLocationsTermSet.GetTerms($newValue, $false)
    $foundTerm = $false
    $newTerm = $null 
    foreach ($term in $terms) {
        Write-Host "  Found: $($term.Name)" -BackgroundColor Yellow -ForegroundColor Black
        $foundTerm = $true
        $newTerm = $term 
    }

    if (-not $foundTerm) {
        $newTerm = $officeLocationsTermSet.CreateTerm($theTermValue, 1033)
        $foundTerm = $true 
        Write-Host "  Created: $($newTerm.Name)" -BackgroundColor DarkGreen -ForegroundColor Black
        $taxStore.CommitAll()
    }

    Write-Host "Adding the term..."
    $locationsCollection.AddTaxonomyTerm($newTerm)
    Write-Host "Term added."
    $user.Commit()
    Write-Host "Profile Committed."
}

The call to AddTaxonomyTerm triggers this exception:

Exception calling "AddTaxonomyTerm" with "1" argument(s): "Index was out of ran
ge. Must be non-negative and less than the size of the collection.
Parameter name: index"
At E:\mark\updateUserProfile.ps1:41 char:41
+     $locationsCollection.AddTaxonomyTerm <<<< ($newTerm)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Any thoughts on why this would occur would be of great help.  Thanks.

May 26th, 2015 9:17pm

Hi Mark,

According to your code, you might want to add more value into a user property.

Based on the error message, the $locationsCollection object seems not be able to hold multiple values.

Is this property the Office Location? By default, it is a property whose type is String and accepts single value only.

As a suggestion, you can try setting a single value for this property to see if it will work in your environment.

A demo about Setting the Values of Properties using PowerShell for your reference:

http://social.technet.microsoft.com/wiki/contents/articles/20692.sharepoint-2013-get-set-and-copy-user-profile-properties-using-powershell.aspx#Getting_the_Value_of_a_Property

Thanks 

Patrick Liang

Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 2:25am

Hi Patrick,

This is "Office Location" but we have it mapped to an open taxonomy term set called Office Location.  When using this via the string propererties:

$user[$propName].Value = "New Location Value"

This works but only if "New Location Value" is not a value in the term set.  If that value already exists, it fails.

May 28th, 2015 1:31pm

Hi Mark,

I map a term set to the Office Location, then use the line below:

$user[$propName].Value = "New Location Value"

to update the value of the Office Location property with a random text or a text value which same as a terms label in the term set, it turns out all works without issue.

As you said, If that value already exists, it fails, is there any error message thrown?

Thanks 

Patrick Liang
Free Windows Admin Tool Kit Click here and download it now
June 3rd, 2015 2:32am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics