1

I have a list which uses a managed metadata column, that is configured to have multiple values.

I need to update the particular items in the list which have a specific term, and change it.

For example, Column1 contains {apple, pear, banana}

I need to remove 'apple' and add 'car' so that Column1 now becomes {car, pear, banana} (order does not matter)

So far I have retrieved the column, and its collection using the code below:

$camlQuery = "<Where>
            <Eq>
                <FieldRef Name='Column1' />
                    <Value Type='Text'>apple</Value>
            </Eq>
          </Where>"

$web = Get-SPWeb http://my
$list = $web.Lists['myList']
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.Query = $camlQuery
$spListItems = $list.GetItems($spQuery)

foreach ($item in $spListItems)
{
  $spItem = [Microsoft.SharePoint.SPListItem]$item
  $file = $item.File

  if($item.Fields.ContainsField("Column"))
  {
    $file.CheckOut()
    $taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields.GetFieldByInternalName("Column")

    $taxFieldCollection = [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$item[$taxField.Title]

    if($taxField.AllowMultipleValues) 
    {
        foreach ($field in $taxFieldCollection)
        {
        }
    }
    $file.Update()
    $file.CheckIn("checked-in", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
  }
}

I have also connected to the metadata service, and retrieved the term that I am required to add to the metadata column using:

$centralAdmin = Get-SPWebApplication -IncludeCentralAdministration | Where {$_.IsAdministrationWebApplication} | Get-SPSite  
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($centralAdmin)  
$serviceApp = Get-SPServiceApplication | Where {$_.DisplayName -eq "Managed Metadata Service"}  
$termStore = $session.TermStores[$serviceApp.Name]  
$termSet = $termStore.Groups["Group"].TermSets["TermSet"].Terms["Terms"]

$terms = $termSet.GetTerms()
$term = $terms | ?{$_.Name -eq $value} #$value is 'car'

return $term

How do I remove the unwanted to term and add the new one? I am a bit stuck at the moment, and are unsure how to proceed.

For reference I have been looking through (Powershell: Cannot update taxonomy type column item with multiple values)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.