1

嘗試更新鏈接到分類術語集的SharePoint用戶配置文件屬性時遇到問題。以下代碼導致異常。使用分類術語更新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 

爲什麼這會發生是有很大幫助的任何想法。謝謝。

+0

你解決了這個問題嗎?據我可以告訴'AddTaxonomyTerm'不適用於空的分類學領域。 –

回答

0

看來你不能在空字段上使用AddTaxonomyTerm方法。你需要首先「初始化」分類學領域。首先使用.Value方法,然後使用AddTaxonomyTerm方法。

$locationsCollection.Value = $newTerm.Name; 
$locationsCollection.AddTaxonomyTerm($newTerm) 
相關問題