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:
- var scriptbaseURL = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";
- $.getScript(scriptbaseURL + "SP.Runtime.js",
- function ()
- {
- $.getScript(scriptbaseURL + "SP.js", function()
- {
- $.getScript(scriptbaseURL + "SP.Taxonomy.js", GetTermSet);
- });
- }
- );
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.
- var ctx = SP.ClientContext.get_current();
- 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".
- var termStores = taxSession.get_termStores();
- 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".
- var groups = termStore.get_groups()
- 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".
- var termSets = group.get_termSets()
- 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.
- ctx.load(termStore);
- ctx.load(termSet);
- ctx.executeQueryAsync(function(){
- termStoreId = termStore.get_id();
- termSetId = termSet.get_id();
- MapTermSet();
- },function(sender,args){
- console.log(args.get_message());
- });
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.

RajaPosted Jun 27, 2016, 12:30 AM
Good one..
Vignesh ManiPosted Jun 26, 2016, 4:45 PM
Nice
Debasis SahaPosted Jun 25, 2016, 2:52 AM
Nice one..
Santhakumar MunuswamyPosted Jun 25, 2016, 12:21 AM
Thank you for nice article
Prasanna MuraliPosted Jun 24, 2016, 9:39 PM
Nice one...
kalu singh raoPosted Jun 24, 2016, 4:00 PM
Nice...