Map Term Set To Metadata Site Column On SharePoint Using JavaScript Object Model

Introduction

In this article, you will learn how to map a SharePoint taxonomy term set to a metadata site column using JavaScript Object model on SharePoint 2013/SharePoint online sites like O365.
 
The basic idea of the article is to show how we can automate the mapping process of the term set to the site columns.
 
Load the required script files with the following snippet: 
  1. var scriptbaseURL = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";  
  2. $.getScript(scriptbaseURL + "SP.Runtime.js",  
  3.     function ()
  4.    {  
  5.         $.getScript(scriptbaseURL + "SP.js"function()
  6.         {    
  7.             $.getScript(scriptbaseURL + "SP.Taxonomy.js", GetTermSet);    
  8.         });  
  9.     }  
  10. ); 
Here, we will see how we can retrieve the term sets from the metadata termstore and assign it to the metadata site column.
 
Get Term Set 
 
Let us first see how we can retrieve the term set from the taxonomy term store, using the term store name and the term set name. 
  • Get the current context of the site. Using taxonomy and taxonomy session objects, get the taxonomy session of the current site context.
    1. var ctx = SP.ClientContext.get_current();  
    2. Session = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);  
  • From the taxonomy session, get the term stores. From the term store collection, retrieve the exact term store by using the term store name. In our case, the term store name is "Managed Metadata Service".
    1. var termStores = taxSession.get_termStores();  
    2. termStore = termStores.getByName("Managed Metadata Service");  
  • From the term store; retrieve the groups. From the groups collection; retrieve the exact group by passing the group name. In our case, the group name is "Testing".  
    1. var groups = termStore.get_groups()  
    2. var group = groups.getByName("Testing")  
  • From the group, retrieve all the term sets. From the term set collection, get the respective term set by passing the term set name. The term set name in our case is "Test".
    1. var termSets = group.get_termSets()  
    2. termSet = termSets.getByName("Test")  
  • Load the term store, the term set objects and execute with the context to retrieve the required values. Here, we require the term store ID and term set ID. These properties are used to assign to the metadata site column properties.
    1. ctx.load(termStore);  
    2. ctx.load(termSet);  
    3. ctx.executeQueryAsync(function(){  
    4.         termStoreId = termStore.get_id();  
    5.         termSetId = termSet.get_id();  
    6.         MapTermSet();  
    7.     },function(sender,args){  
    8.   
    9.          console.log(args.get_message());  
    10.   
    11. });  
Here, the names are used for the term store and the term set instead of IDs, because the term store ID and term set id will change from Server to Server and the names will be the same. The names are common, because the users add it to the taxonomy store. 
 
Mapping Term Set to Site Column
 
Now, we will see how we can map the above term set to the site column.
  • Get the current context of the site, retrieve the Web object and from the object, get the fields collection.
    1. var ctx = SP.ClientContext.get_current();  
    2. var web = ctx.get_web();  
    3. var fields = web.get_fields();  
  • Using field collections, retrieve the exact column by name with getByName method. In this case, column name is "TestColumn".
    1. field = fields.getByInternalNameOrTitle("TestColumn");  
  • Cast the field to the taxonomy field. In the taxonomy field, set the term set ID and the term store ID of the term set. (Use values from the section, given above).
    1. txField = ctx.castTo(field, SP.Taxonomy.TaxonomyField);  
    2. txField.set_sspId(termStoreId);  
    3. txField.set_termSetId(termSetId);  
  • Update and execute the query with the context to finalize the changes.
    1. txField.update();  
    2. ctx.executeQueryAsync(function(){                        
    3. },function(sender,args){      
    4.       console.log(args.get_message());    
    5. });  
The above operation will just set the term set to metadata site column at the site level. The columns inherited at the list level will not be changed. To make the changes of the site columns at the list level, we need to use the updateAndPushChanges(true), instead of update() method.
 
Now, if you open the site column, you will see the term set called "Test" is mapped to the site column "TestColumn" in the term set settings section.
 
The code snippet given below shows the entire view for the operations, mentioned above.
  1. var scriptbaseURL = siteurl + "/_layouts/15/";  
  2. $.getScript(scriptbaseURL + "SP.Runtime.js",  
  3.     function () {  
  4.         $.getScript(scriptbaseURL + "SP.js"function(){  
  5.             $.getScript(scriptbaseURL + "SP.Taxonomy.js", GetTermSet);  
  6.         });  
  7.     }  
  8. );  
  9. var termStore = "";  
  10. var termStoreId = "";  
  11. var termSet = "";  
  12. var termSetId = "";  
  13. function GetTermSet(){  
  14.     var ctx = SP.ClientContext.get_current();  
  15.     var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);  
  16.     var termStores = taxSession.get_termStores();  
  17.     termStore = termStores.getByName("Managed Metadata Service");  
  18.     var groups = termStore.get_groups()  
  19.     var group = groups.getByName("Testing")  
  20.     var termSets = group.get_termSets()  
  21.     termSet = termSets.getByName("Test")  
  22.       
  23.     ctx.load(termStore);  
  24.     ctx.load(termSet);  
  25.     ctx.executeQueryAsync(function(){  
  26.             termStoreId = termStore.get_id();  
  27.             termSetId = termSet.get_id();  
  28.             MapTermSet();  
  29.         },function(sender,args){  
  30.           console.log(args.get_message());  
  31.     });  
  32. }  
  33. var field = "";  
  34. var txField = "";  
  35. function MapTermSet(){  
  36.     var ctx = SP.ClientContext.get_current();  
  37.     var web = ctx.get_web();  
  38.     var fields = web.get_fields();  
  39.     field = fields.getByInternalNameOrTitle("TestColumn");  
  40.     txField = ctx.castTo(field, SP.Taxonomy.TaxonomyField);  
  41.     txField.set_sspId(termStoreId);  
  42.     txField.set_termSetId(termSetId);  
  43.     //txField.update();  
  44.     txField.updateAndPushChanges(true);  
  45.     ctx.executeQueryAsync(function(){                         
  46.     },function(sender,args){      
  47.           console.log(args.get_message());    
  48.     });  
  49.  
Summary
 
Thus, you have learned how to retrieve and set the term set from the managed metadata store to a metadata site column, using JavaScript Object Model. This is also applicable for SharePoint 2013 or SharePoint online sites.