Change MMS Field Term Set Default value using CSOM and PowerShell

The script below allows you to change Term Column Default Value in SharePoint Online using Client Side Object Model.  One of my client used an MMS Field in multiple libraries without using Site Column so I had to write this script to change the default value.  The script is iterating all sites and sub sites to find the fix the column.  It is a best practice to use Site Column.  You must use latest CSOM Components from Github for better performance.

$Creds = Get-Credential
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll”

$Databases = $null
$Databases = @();

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext(“https://tenant.sharepoint.com”)
#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Creds.UserName , $Creds.Password)
$ctx.Credentials = $credentials

#Fetch the users in Site Collection
$ctx.Load($ctx.Web)
$ctx.Load($ctx.Web.Webs)
$Lists = $ctx.Web.Lists
$ctx.Load($Lists)
$ctx.ExecuteQuery()

$taxStore = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($ctx) 
$ctx.Load($taxStore) 
$ctx.ExecuteQuery() 

$termStore = $taxStore.TermStores.GetByName(“Taxonomy_H2lgNrFxKmSlqqMA0l4JFw==”)
$Groups = $termStore.Groups 
$ctx.Load($termStore) 
$ctx.Load($Groups) 
$ctx.ExecuteQuery()

$Group = $Groups.GetByName(“Site Collection – tenant.sharepoint.com”) 
$termSet = $Group.TermSets.GetByName(“PrimaryTags”) 
$ctx.Load($termSet) 
$ctx.ExecuteQuery() 

foreach($List in $Lists)
{
  if($List.Hidden -eq $false)
  {
      if($Title -eq “Site Assets” -or $Title -eq “Master Page Gallery”)
      { continue }
      if($List.BaseType -eq “DocumentLibrary”)
      {
            $Fields = $List.Fields
            $ctx.Load($Fields)
            $ctx.ExecuteQuery()
            $PrimaryTag = $Fields | Where {$_.Title -eq “Primary Tag”}
            if($PrimaryTag)
            {
              $field = [Microsoft.SharePoint.Client.ClientContext].GetMethod(“CastTo”).MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($ctx, $PrimaryTag)
              #$field.SspId = $termStore.Id
              #$field.TermSetId = $termSet.Id
              #$field.Update()
              #$ctx.ExecuteQuery()
            }
      }
  }
}
foreach($aWeb in $ctx.Web.Webs)
{
    $Lists = $aWeb.Lists
    $ctx.Load($Lists)
    $ctx.ExecuteQuery()
    foreach($List in $Lists)
    {
        $Title = $List.Title
        if($Title -eq “Site Assets” -or $Title -eq “Master Page Gallery”)
        { continue }
        if($List.BaseType -eq “DocumentLibrary”)
        {
            $Fields = $List.Fields
            $ctx.Load($Fields)
            $ctx.ExecuteQuery()
            $PrimaryTag = $Fields | Where {$_.Title -eq “Primary Tag”}
            if($PrimaryTag)
            {
              $field = [Microsoft.SharePoint.Client.ClientContext].GetMethod(“CastTo”).MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($ctx, $PrimaryTag)
              if(!$field.DefaultValue.Contains(“#Project”))
              {
                $field.DefaultValue = “44;#Project Management|bf247376-f948-45b0-9dc6-69b94e9978f4”
                $field.Update()
              }
            }
            Write-Host $aSite.Url $List.Title $List.ItemCount
        }
    }
  }

Update: The script is also available

https://gallery.technet.microsoft.com/The-script-below-allows-31b6ee7f

One thought on “Change MMS Field Term Set Default value using CSOM and PowerShell

  1. Great!

    Is there any updates regarding “How to update the taxonomy Hidden List or how to force rerun the Taxonomy Update scheduler job to Sync updates to Taxonomy Hidden list”

    Regards,
    Mohammad Amer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s