We will see how to map SharePoint taxonomy term set to a Site column using CSOM with PoweShell on SharePoint 2013 / SharePoint 2016 / SharePoint Online sites.
The basic idea of the article is to show how we can automate the mapping process of term set to site columns.
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 necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Taxonomy dll.
- Initialize client context object with the site URL.
- If you are trying to access SharePoint Online site then you need to setup the site credentials with credentials parameter and load it to the client context.
- If you are trying to access the SharePoint on premise site then 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.
Before executing below steps, the site column’s Term Setting will look like the image below.
Now we will see how we can retrieve term sets from the term store and assign it to the site column.
Retrieve Term Sets:
The below logic retrieves term set called "Test" from the taxonomy term store.
- Get taxonomy session object by loading and executing the context.
- Get the term store by using term store name and term store collection object.
- Get the appropriate group where term sets are available by using group name and group collection object. From the selected group, select the term set from the term set collection and term set name.
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:
- From the context, get the root web object. Then get the fields. From the field collection, get the respective field with the field name.
- We need to cast the field object to taxonomy field by invoking generic methods. For this we will use MakeGenericMethod along with GetMethod. Here we will use Taxonomy Field object for casting field object.
- Then we can assign the term set and store id of the term set identified from the above section to the field by following code snippet.
- Then update field, load objects and execute the query to complete processing.
Here we have mapped the term set to corresponding site column. The below image shows the term set mapping to site column.
The above set of operations will help in mapping the term set of taxonomy to managed metadata site column. But this will not affect the columns already created at list level using the above site column. 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 site level to list levels. Instead of giving $field.update() in the above code, we need to give $field.UpdateAndPushChanges($true). This will make changes available for list columns already created.