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.

Get top level parent of Term in SharePoint Taxonomy using C#

While working with SharePoint taxonomy sometimes we come across the problem where we want to get the top level parent of the given TermId. So here i am sharing the way of getting the top level parent term using given Term Id using c# code.

For the implementation of the code you will need to include Microsoft.SharePoint.Taxonomy library.

The below given code you can keep on any event action when you want to get the parent term.


try
{
HttpContext oldContext = HttpContext.Current;
HttpContext.Current = null;
string TermId = "Your Term Id"
Term ParentTerms = null;
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite site = new SPSite("Your site URL"))
{
TaxonomySession taxonomySession = new TaxonomySession(site);
TermStore termStore = taxonomySession.TermStores["Your Managed MeataData Service Name"];
Group group = termStore.Groups["Your Termstore Group Name"];
TermSet sectionTermset = group.TermSets["Your Termset Name"];
ParentTerms = getParentTerm(sectionTermset, TermId);

/* “ParentTerms” is the object of your term, so using this you can get the details of the term like name, Id etc
}
});
}
catch (Exception ex)
{
// your catch code
}

Here the method “getParentTerm()” is called recursively to get the top level parent. The definition for the method is as below:-


public Term getParentTerm(TermSet termSet, string termGuid)
{
Term parentSectionTerm = null;
Term newParentSectionTerm = null;
parentSectionTerm = termSet.GetTerm(new Guid(termGuid));
var isTopLevelParent = parentSectionTerm.IsRoot;
if (!isTopLevelParent)
{
newParentSectionTerm = parentSectionTerm.Parent;
getParentTerm(termSet, Convert.ToString(newParentSectionTerm.Id));
}
else
{
newParentSectionTerm = parentSectionTerm;
}
return newParentSectionTerm;
}

Here “IsRoot” is the property of the term which returns true/false, if the term does not have any parent then it will return ‘true’, and if the term has the parent then it will return ‘false’.

I hope this article will help you to get rid of your problem for getting top level parent of the Term