Recently I’ve been having a hard time dealing with managed metadata default values. I’ve been trying to set default values with a piece of code from SharePoint 2010. I guess this is one out of few places where things got changed from previous version.
First things first – scenario: Users add an item to the list. Then an event receiver creates a site collection. 2 managed metadata fields in the newly created site need to have default values set.
This was doable with a fewer lines of code in SharePoint 2010:
1. Get the term and format a default value. Notice the “-1;#” in String.Format(). This is what causes problems in SharePoint 2013:
2. Assign it to the DefaultValue property of the field and update it:
SharePoint 2013 in this case doesn’t like the “-1;#” anymore. This should be replaced with a WssId – a unique term ID from TaxonomyHiddenList. This is unique for every site collection. We need to get that before assigning a default value:
1. Get the taxonomy field which will have the default value:
2. Get the term which will be used as a default:
3. Get the WssId, format a default value string, assign it and update the field:
Hope this helps!