top of page

Map Term Set To Site Column On SharePoint Using CSOM PowerShell


Introduction

In this article, you will learn how to map a SharePoint taxonomy term set to a site column using CSOM with PowerShell on SharePoint 2013/SharePoint online sites. The basic idea of the article is to show how we can automate the mapping process of the term set to the site columns.


Steps Involved

The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.

  • Add the references, using the Add-Type command with the necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Taxonomy dll, as shown below:

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"

  • Initialize the client context object with the site URL, as shown below:

$siteURL = "" $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

  • If you are trying to access the SharePoint online site, you need to set up the site credentials with the credentials parameter and load it to the client context.

# Not required for on premise site - Start  

$userId = "abc@abc.onmicrosoft.com"

$pwd = Read-Host -Prompt "Enter password" -AsSecureString    

$creds = NewObject Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)    

$ctx.credentials = $creds   

  # Not required for on premise site - End  

  • If you are trying to access SharePoint on the premise site, the credentials parameter is not required to be set to the context but you need to run the code on the respective SharePoint Server or you need to pass the network credentials and set the context.

# Credentials for on premise site - Start  

$pwd = Read-Host -Prompt "Enter password" -AsSecureString    

$creds = New-Object System.Net.NetworkCredential("domain\userid", $pwd)    

$ctx.Credentials = $creds    

# Credentials for on premise site - End  


Before executing the steps given below, the site column’s term setting will look like the image, shown below:



Now, we will see, how we can retrieve the term sets from the term store and assign it to the site column 


Retrieve Term Sets

The logic given below, retrieves the term set called "Test" from the taxonomy term store. 

  • Get taxonomy session object by loading and executing the context.

$taxStore = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::

GetTaxonomySession($ctx)  

$ctx.Load($taxStore)  

$ctx.ExecuteQuery()  

  • Get the term store by using the term store name and the term store collection object.

# Retrieve Term Stores & Groups $termStores = $taxStore.TermStores.GetByName("Managed Metadata Service")  

$ctx.Load($termStores)  

$ctx.ExecuteQuery()  

$termStore = $termStores[0]  

$Groups = $termStore.Groups  

$ctx.Load($termStore)  

$ctx.Load($Groups)  

$ctx.ExecuteQuery()  

  • Get the appropriate group where the term sets are available by using a group name and group collection object. From the selected group, select the term set from the term set collection and term set name.

# Retrieve Term Set & Update Site Column with TermSet

$Group = $Groups.GetByName("Testing")  

$termSet = $Group.TermSets.GetByName("Test")  

$ctx.Load($termSet)  

$ctx.ExecuteQuery()  


We got the term set. Now we will see how we can map it to site column called "Test". 


Mapping Term Set to Site Column

1. From the context, get the root Web object. Afterwards, get the fields. From the field collection, get the respective field with the field name.

$ web = $ctx.Site.RootWeb  $field = $web.Fields.GetByTitle("Test")  


2. We need to cast the field object to taxonomy field by invoking the generic methods. For this, we will use MakeGenericMethod along with GetMethod. Here, we will use Taxonomy field object for casting the field object.

$field = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo"). MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($ctx, $field)  


3. Afterwards, we can assign the term set and store ID of the term set identified from the section, given above to the field by following the code snippet, given below:


$field. SspId = $termStore. Id   

$field.TermSetId = $termSet. Id   


4. Update the field, load the objects and execute the query to complete processing.


$field.Update()  

$ctx.Load($web)  

$ctx.Load($field)  

$ctx.ExecuteQuery()  

 

Here, we have mapped the term set to corresponding site column. The image, given below, shows the term set mapping to the site column.



The above set of the operations will help in mapping the term set of taxonomy to manage metadata site column. This will not affect the columns already created at the list level using the site column mentioned above. For updating the list level metadata columns with the same changes, we need to explicitly use an update operation, where it will push the changes from the site level to the list levels. Instead of giving $field.update() in the code, given above, we need to give $field.UpdateAndPushChanges($true). This will make changes available for list columns, already created.


Summary

Thus, you have learned how to map the term set from the managed metadata term store to metadata site columns.

0 comments

Comments


bottom of page