0

Below is my code. Currently it updates all the taxonomy field values in the document library. I just need to set a condition where Taxonomy column value(document library) is equal to "Termvalue2" then change to "TermValue1"

$taxonomySession = Get-SPTaxonomySession -site 

$termStore = $taxonomySession.TermStores["Managed Metadata Service"]

$termStoreGroup = $termStore.Groups["MyTermGroup"]

$termSet = $termStoreGroup.TermSets["Mytermset"]

$t = $termset.GetAllTerms()

$term = $t["TermValue1"]

##############################

$Site = Get-SPWeb 

$List = $Site.Lists["Site Documents"]
$taxField =[Microsoft.SharePoint.Taxonomy.TaxonomyField]$List.Fields["MyField"]
$taxField.GetFieldValue()

foreach($i in $list.Items)
{
  $var = $listItems
  $taxField.SetFieldValue($i, $term) 
  $i.update();
} 

3 Answers 3

1

You could add the condition to check if the value of the managed metadata column is equal to “Termvalue2” in the loop foreach.

There is a demo for your reference. The “mm” is the managed metadata column in my document library.

Get-SPTaxonomySession -site http://sp:5872/
$taxonomySession = Get-SPTaxonomySession -site http://sp:5872/
$termStore = $taxonomySession.TermStores["Managed Metadata Service"]
$termStoreGroup = $termStore.Groups["People"]
$termSet = $termStoreGroup.TermSets["Department"]
$t = $termset.GetAllTerms()
$term = $t["1"]

$Site = Get-SPWeb http://sp:5872/
$list = $Site.Lists["doc1"]
$taxField =[Microsoft.SharePoint.Taxonomy.TaxonomyField]$List.Fields["mm"]
foreach($i in $list.Items)
{
 $taxFieldValue = $i["mm"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue];            
 $taxFieldValue.Label;
  if($taxFieldValue.Label -eq "test3")
  {
    $taxField.SetFieldValue($i, $term); 
    $i.update();
  }
}
1

I was able to solve the issue with below code:

https://social.msdn.microsoft.com/Forums/office/en-US/ab7464f8-510f-4932-8240-eff5b6e24a0f/powershell-to-change-taxonomy-field-value-with-condition?forum=sharepointdevelopment#91ccd658-a842-460c-b2ca-201c14195d12

Add-PSSnapin Microsoft.SharePoint.PowerShell
#Variables
$WebURL="http://sp:12001"
$ListName="Documents"
$FieldName="MyField"

#Get the web
$Web = Get-SPWeb $WebURL    

#Get the Term from Term store
$TaxonomySession = Get-SPTaxonomySession -Site $web.Site
$TermStore = $TaxonomySession.TermStores["Mananged Metadata"]
$TermGroup = $TermStore.Groups["MyTermGroup"]
$TermSet = $TermGroup.TermSets["Mytermset"]
$Term = $Termset.Terms["TermValue1"]

#Use SharePoint Manager

#Get the List and List Item
$List= $Web.Lists[$listName]
foreach($i in $list.Items)
{   
    $taxFieldValue = $i["MyField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue];            
    $taxFieldValue.Label;   
    if($taxFieldValue.label -eq "termvalue2")
    {
        $taxField.SetFieldValue($i, $term) 
        $i.update();
    }
}

Write-host "Managed Metadata Field value updated!"
0

This fixed it for me. I abandoned using the SetFieldValue altogether and did it this way instead:

$session = Get-SPTaxonomySession -Site $sList.ParentWeb.Site  
          
$termStore = $session.TermStores["My Managed Metadata"]
    
#get taxanomy group and termset

$termStoreGroup = $termStore.Groups["Enterprise Taxonomy"]

$termSetFiscalYear = [Microsoft.SharePoint.Taxonomy.TermSet]

$termStoreGroup.TermSets["Fiscal Year"]
    
#get all terms for Fiscal year

$t = $termSetFiscalYear.GetAllTerms()

$termSetFiscalYearValueToSet = $t["0000-0000"]

foreach($spItem in $sListItems)
{

   $listTaxonomyFieldFiscalYear = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$sItem.Fields["FiscalYear"]

   $termString = $termSetFiscalYearValueToSet.Name + "|" + $termSetFiscalYearValueToSet.Id + ";"  

   $sItem["FiscalYear"] = $termString.Substring(0,$termString.Length-1) 

   $sItem.Update()

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.