1

分類用語セットにリンクされている 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

なぜこれが発生するのかについての考えは、非常に役立ちます。ありがとう。

4

1 に答える 1

0

AddTaxonomyTerm空のフィールドではメソッドを使用できないようです。最初に分類フィールドを「初期化」する必要があります。.Value最初にメソッドを使用し、次にメソッドを使用しAddTaxonomyTermます。

$locationsCollection.Value = $newTerm.Name;
$locationsCollection.AddTaxonomyTerm($newTerm)
于 2015-09-16T08:30:40.223 に答える