Wednesday, July 19, 2017

PowerShell – Get the Sharepoint Manange Meta Data site colum default value and set that value in all list under that site.


Add-PSSnapin Microsoft.SharePoint.Powershell

$site  = 'Site URL’
$fieldName = 'Countries'
$web = Get-SPWeb $site
$mmsServiceName = "Managed Metadata Service"
$taxonomySite = Get-SPSite $site
$web=  $taxonomySite.OpenWeb()
$taxSession = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($taxonomySite)
$termStore = $taxSession.TermStores[$mmsServiceName]
$field = $web.Fields.GetFieldByInternalName('Countries')
$defaultValue=$field.DefaultValue

foreach($list in $web.Lists)
     {
      if( $list.Fields.ContainsField("Countries") -eq $true)
           {                                      
               write-host $item["Title"]
               $spItem = [Microsoft.SharePoint.SPListItem]$item;
               $taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields["Countries"]
               $item["Countries"] = $defaultValue;
               $item.Update();
               break;
           }
     }

No comments:

Post a Comment