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.
Get the current context of the site, retrieve the Web object and from the object, get the fields collection.
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var fields = web.get_fields();
Using field collections, retrieve the exact column by name with getByName method. In this case, column name is "TestColumn".
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).
txField = ctx.castTo(field, SP.Taxonomy.TaxonomyField);
txField.set_sspId(termStoreId);
txField.set_termSetId(termSetId);
Update and execute the query with the context to finalize the changes.
txField.update();
ctx.executeQueryAsync(function(){ },
function(sender,args){
console.log(args.get_message());
});
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.
var scriptbaseURL = siteurl + "/_layouts/15/";
$.getScript(scriptbaseURL + "SP.Runtime.js",
function () {
$.getScript(scriptbaseURL + "SP.js",
function(){
$.getScript(scriptbaseURL + "SP.Taxonomy.js", GetTermSet);
});
} );
var termStore = "";
var termStoreId = "";
var termSet = "";
var termSetId = "";
function GetTermSet(){
var ctx = SP.ClientContext.get_current();
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);
var termStores = taxSession.get_termStores();
termStore = termStores.getByName("Managed Metadata Service");
var groups = termStore.get_groups()
var group = groups.getByName("Testing")
var termSets = group.get_termSets()
termSet = termSets.getByName("Test")
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());
});
}
var field = "";
var txField = "";
function MapTermSet(){
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var fields = web.get_fields();
field = fields.getByInternalNameOrTitle("TestColumn");
txField = ctx.castTo(field, SP.Taxonomy.TaxonomyField);
txField.set_sspId(termStoreId);
txField.set_termSetId(termSetId);
// txField.update();
txField.updateAndPushChanges(true);
ctx.executeQueryAsync(function(){ },
function(sender,args){
console.log(args.get_message());
});
}
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.
Comments