Managed metadata field: Set default value on library by powershell
-
26-01-2021 - |
سؤال
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.
المحلول
Well, I did not find any other options and help, so I started using PnP, and this worked for me:
Connect-PnPOnline -Url http://whateveronprem
Set-PnPDefaultColumnValues -List "Dokumenter" -Field "Tema" -Value "dxxxxx-xxx-yy-sad1-xyzc59365bd0"
For multiple values, use this:
$Taxvalue = "15c4c4e4-4b67-4894-a1d8-de5ff811c791","16c4c4e4-4b67-4894-a1d8-de5ff811c792"
Set-PnPDefaultColumnValues -List Documents -Field TaxKeyword -Value $Taxvalue
"No other way is accepted since for the taxonomy fields. If you use a semicolon ";" it does not get split. For the other value types the semicolon does work."