I am trying to set default values (multiple) on a managed metadata field on a document library, which I manually would do like this in the GUI: Library settings > Column default value settings, and then chose a termset and set default values on that.
This is my (very unprofessional) code where I get the terms from a page field, and try to set what I get as default column values on the document library:
$url = "http://dev.xxxx";
$site = Get-SPSite $url
$pageTitleToChange = "Test"
$siteColumnTemaForArbeidsrom = "Tema";
$spWeb = Get-SPWeb -Identity $url;
$spPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb);
$pages = $spPubWeb.PagesList
foreach($pitem in $pages.Items){
$pubPage = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($pitem)
if ($pubPage.Title -eq $pageTitleToChange){
$multiplemanagedmetadatafield = $pitem.Fields[$siteColumnTemaForArbeidsrom] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField];
$taxFieldValueCollection = $pitem[$siteColumnTemaForArbeidsrom] -as [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection];
$list = $site.RootWeb.Lists["Dokumenter"]
if($list -ne $null){
$field = $list.Fields["Tema"]
foreach($t in $taxFieldValueCollection){
$label = $t.Label
$termguid = $t.TermGuid
$value = ("1033;#" + $label + "|" + $termguid)
Write-Host $value
$field.DefaultValue = $value #$taxFieldValueCollection #$multiplemanagedmetadatafield
$field.Update()
}
}
}
}
The result from this code is that it sets last term found in the collection as a default value on the document library, but when I try to upload documents in that library the default value is not set even though when I see "Change Default Column Value" in the GUI the value is there.
Any ideas on how I can set multiple terms as default column values, and make the script work properly?
Our platform is SP 2016 on-prem with feature pack 2.