使用分类术语更新SharePoint用户配置文件属性

时间:2015-05-22 15:22:25

标签: powershell sharepoint sharepoint-2010 sharepoint-userprofile

我在尝试更新链接到分类法TermSet的SharePoint用户配置文件属性时遇到问题。以下代码导致异常。

$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."
}

对AddTaxonomyTerm的调用会触发此异常:

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

对于为什么会发生这种情况的任何想法都会有很大的帮助。感谢。

1 个答案:

答案 0 :(得分:0)

您似乎无法在空字段上使用AddTaxonomyTerm方法。你需要&#34;初始化&#34;首先是分类学领域。首先使用.Value方法,然后使用AddTaxonomyTerm方法。

$locationsCollection.Value = $newTerm.Name;
$locationsCollection.AddTaxonomyTerm($newTerm)