Request a Quote

SharePoint Taxonomy examples using JavaScript

SharePoint Taxonomy examples using JavaScript

SharePoint 2013 has introduced new feature for working with Managed Metadata using JavaScript. For working with SharePoint Taxonomy you will need to load Sp.Taxonomy.js explicitly as SharePoint doesn’t load it by default.

You will also need the SP.js and SP.Runtime.js before working with SharePoint Taxonomy to avoid very common errors like “SP.Taxonomy is undefined” or “Unable to get property ‘TaxonomySession’ of undefined or null reference“.

Lets have some basic SharePoint Taxonomy examples using JavaScript

Get all terms under given TermSet

// get all terms under particular TermSet
$(document).ready(function () {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", function () {
$.getScript(scriptbase + "SP.Taxonomy.js", getAllTermsFromTermSet);
});
});
});
// get all the terms
function getAllTermsFromTermSet() {//defines current context
var context = SP.ClientContext.get_current();//defines current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

//Term Stores
var termStores = taxSession.get_termStores();

//get the Termstore by Termstore name
var termStore = termStores.getByName(“Your_Taxonomy_Name”);

// get the termset by GUID
var termSet = termStore.getTermSet(“XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX”);

// get all terms from termset
var terms = termSet.getAllTerms();

// load all the terms
context.load(terms);

context.executeQueryAsync(function () {
var termEnumerator = terms.getEnumerator();

// iterate through terms
while (termEnumerator.moveNext()) {

// get current term details
var currentTerm = termEnumerator.get_current();
console.log(“Name” + currentTerm.get_name()); // You can also retive other information about term
}
}, function (sender, args) {
console.log(args.get_message());
});
}

Create new term group under given term store
$(document).ready(function () {var scriptbase = _spPageContextInfo.webServerRelativeUrl + “_layouts/15/”;

$.getScript(scriptbase + “SP.Runtime.js”,
function () {
$.getScript(scriptbase + “SP.js”, function () {

$.getScript(scriptbase + “SP.Taxonomy.js”, createTermGroup);

});
});
});

function createTermGroup() {

//defines current context
var context = SP.ClientContext.get_current();

//defines current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

//Term Stores
var termStores = taxSession.get_termStores();

//get the Termstore by Termstore name
var termStore = termStores.getByName(“Your_Taxonomy_Name”);

//Term group name GUID
var newGroup = termStore.createGroup(“Your_Group_Name”, “XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX”); // You can choose your own GUID
context.load(newGroup);
context.executeQueryAsync(function () {
console.log(newGroup.get_name());
}, function (sender, args) {
console.log(args.get_message());
});
}

Create new Termset under given TermGroup

// create new term set under given term group
$(document).ready(function () {var scriptbase = _spPageContextInfo.webServerRelativeUrl + “_layouts/15/”;$.getScript(scriptbase + “SP.Runtime.js”,
function () {
$.getScript(scriptbase + “SP.js”, function () {

$.getScript(scriptbase + “SP.Taxonomy.js”, createTermSet);

});
}
);

});

function createTermSet() {

//defines current context
var context = SP.ClientContext.get_current();

//defines current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

//Term Stores
var termStores = taxSession.get_termStores();

//get the Termstore by Termstore name
var termStore = termStores.getByName(“Your_Taxonomy_Name”);

//Get group by GUID
var termGroup = termStore.getGroup(“XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX”); // GUID of your term group

//Create New Term Set in Group with Name, New GUID and LCID
var newTermSet = termGroup.createTermSet(“Your-TermSet_Name”, “XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX”, 1033); // You can give new GUID

context.load(newTermSet);

context.executeQueryAsync(function () {

console.log(newTermSet.get_name());

}, function (sender, args) {

console.log(args.get_message());

});
}

Create new Term under given TermSet
// create new term under termset
$(document).ready(function () {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + “_layouts/15/”;$.getScript(scriptbase + “SP.Runtime.js”,
function () {
$.getScript(scriptbase + “SP.js”, function () {$.getScript(scriptbase + “SP.Taxonomy.js”, createNewTerm);

});
}
);

});

function createNewTerm() {

//defines current context
var context = SP.ClientContext.get_current();

//defines current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

//Term Stores
var termStores = taxSession.get_termStores();

//get the Termstore by Termstore name
var termStore = termStores.getByName(“Your_Taxonomy_Name”);

//Term Set under which to create the term.
var termSet = termStore.getTermSet(“XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX”); // your termset GUID

//Name of the term, LCID and a new GUID for the term.
var newTerm = termSet.createTerm(“termName”, 1033, “XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX”);

//newTerm.set_isAvailableForTagging(true);

context.load(newTerm);

context.executeQueryAsync(function () {

console.log(“Term Created: ” + newTerm.get_name());

}, function (sender, args) {

console.log(args.get_message());

});

}

I hope this will help you to work with basics of Managed Metadata using JavaScript.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2015 Trimantra Software Solution LLP- Software Outsourcing Company India. All rights reserved.