4

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...

3 Answers 3

3

Another way to set metadata field value to null. You should get TaxonomyField first.

TaxonomyFieldValue fieldValue = new TaxonomyFieldValue(field);

field.SetFieldValue(item,fieldValue);

Item.UpdateOverwriteVersion();
1
  • Using SPField.SetFieldValue(item, value) was the only way to modify an existing TaxonomyFieldValue. Commented Jun 6, 2012 at 0:39
1

As reported here by Bhavya Chhabra, this is how the managed metadata field value can be set to null (using C#):

TaxonomyFieldValue managedMetadataFieldValue = item[field.Id] as TaxonomyFieldValue;
managedMetadataFieldValue.WssId = 0;
managedMetadataFieldValue.TermGuid = null;
managedMetadataFieldValue.Label = null;

TaxonomyField managedMetadataField = item.Fields[field.Id] as TaxonomyField;
managedMetadataField.SetFieldValue(item, managedMetadataFieldValue);
1
  • I didn't have luck with this approach when using it in an event receiver because managedMetadataFieldValue was always null. Commented Mar 3, 2012 at 23:56
1

The solution obove does not work for me but I get another solution:

TaxonomyField managedMetadataField = item.Fields[fieldGuid] as TaxonomyField;
TaxonomyFieldValueCollection managedMetadataFieldValue =
               (TaxonomyFieldValueCollection)managedMetadataField.GetFieldValue("");
managedMetadataField.SetFieldValue(item, managedMetadataFieldValue);

Your Answer

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