2013-01-16 28 views
0

我試圖編寫一個PowerShell腳本,查看用戶定義的變量並更新列表中與該值匹配的每個項目的企業關鍵字。如何使用PowerShell更新具有多個值的SharePoint企業關鍵字?

例如,假設您在SP中有一個包含託管元數據關鍵字的頁面:new,fresh,clean 我想要一個腳本來詢問用戶他們想換出什麼關鍵字。因此,用戶可以將變量指定爲:新鮮變量和另一個變量:更新鮮,它將更新任何關鍵字爲新鮮的項目以更新鮮。

這裏是我以前使用過,但因爲有複式值現在無法正常工作:

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue 
$webURL = <MY SP URL> 
$listName = <MY LIST NAME> 
$web = Get-SPWeb $webURL 
$list = $web.Lists[$listName] 
$listitems = $list.items 
$session = Get-SPTaxonomySession -Site $web.Site 
$termStore = $session.TermStores["Managed Metadata Service"] 
$group = $termStore.Groups["Resources"] 
$termset = $group.TermSets["Wiki Categories"] 
$terms = $termSet.GetTerms(100) 
$wiki1 = read-host "Enter the wiki category you want to update" 
$wiki2 = read-host "Enter the replacement wiki category" 

$term = $terms | ?{$_.name -eq $wiki2} 


Foreach($item in $listitems) 
{$wiki = $item["Wiki Categories"] 
    if($wiki.label -eq $term) 
    { 
     $spitem = [Microsoft.SharePoint.SPListItem]$item; 
    $taxfield = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spitem.Fields["Wiki Categories"] 
     $taxfield.SetFieldValue($spitem, $term) 

    $spitem.Update() 
    $spitem.File.Publish("True") 
    } 
} 

我敢肯定的問題是這一行:

$term = $terms | ?{$_.name -eq $wiki2} 

而且這條線:

$taxfield.SetFieldValue($spitem, $term) 
+0

有一個工具,將在www.qipoint.com – 2013-02-26 22:11:49

回答

0

的問題是,你傳遞一個TermCollection($項),以SetFieldValue你應該通過TaxonomyFieldValueCollection

你可以將它們轉換是這樣的:

$taxfield = $spitem.Fields["Wiki Categories"] 

$tfvc = new-object -typename Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection -argumentlist $taxfield; 

foreach($t in $ term) 
{ 
    $tfv = new-object -typename Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue -argumentlist $taxfield 
    $tfv.TermGuid = $t.Id 
    $tfv.Label = $t.Name 
    $tfvc.Add($tfv) 
} 
... 
$taxfield.SetFieldValue($spitem, $tfvc) 
+0

做到這一點,這是否會發生在自己的foreach每個內或在$項目前? – dchess

+0

'...'之前的部分將$ term中的多個值轉換爲不同的集合類型($ tfvc),因此可以在foreach之前爲所有項目執行一次。然後將第二個參數替換爲'$ taxfield.SetFieldValue'。 –

相關問題