In my recent adventures populating Managed Metadata values (aka TaxonomyFieldValue), I have encountered either a bug, potential oversight, or a substantial erosion of my sanity. In short, it does not seem possible to nullify (clear) a list item's TaxonomyFieldValue.
In my travels I have encountered two main methods of populating metadata via code (in my case, Powershell):
- Direct assignment of an item field value, like
$item[$mmField] = $newTaxonomyFieldValue
- Or alternatively, using the SetFieldValue() function in a $TaxonomyField object.
$taxoField.SetFieldValue($item, $term)
For populating fields that are initially empty, both methods work fine.
For updating fields that already have a value, only the second method works - the first fails without any exception being thrown, and no messages in the ULS log.
To clear a value, neither method seems to work!
I can't seem to discern any guidance from MSDN or Google, so I turn to you. What am I missing here?
Edit:
I finally found a reference to the "TaxHTField" hidden field (type: string) which contains a delimiter and term ID. This field can be updated with a new term ID, which then propagates to the associated Managed Metadata column, allowing us to update previously set values:
foreach ($field in $item.Fields) {
if ($field.StaticName.StartsWith($colName) -and $field.StaticName.Contains("TaxHTField")) {
$item[$field.StaticName] = "|" + $term.Id
}
}
Unfortunately, this mechanism does not seem to be nullable either; setting the $item[$field.StaticName] = $null or = "" does not work...