Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

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();

share|improve this answer
    
Thanks mickey, this approach worked great for me. –  George Durzi Mar 4 '12 at 0:00
    
Using SPField.SetFieldValue(item, value) was the only way to modify an existing TaxonomyFieldValue. –  Daniel Little Jun 6 '12 at 0:39
add comment

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);
share|improve this answer
    
I didn't have luck with this approach when using it in an event receiver because managedMetadataFieldValue was always null. –  George Durzi Mar 3 '12 at 23:56
add comment

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);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.