In SharePoint 2010, it was not possible to work with taxonomy types using javascript object model. SharePoint 2013 has introduced a new feature to work with taxonomy by using SP.Taxonomy.js
Here are some basic operations of taxonomy using javascipt object model.
1. Create a taxonomy Term Group
2. Create a taxonomy Term Set
3. Create taxonomy Terms
4. Read taxonomy group, termset and terms.
var groupGuid="cd90a241-2737-48f7-b5a3-43fab9cdc0cb";
var termSetGuid="552ae870-07ce-4934-b5c7-c068022a6873";
jQuery(function($) {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", function(){
$.getScript(scriptbase + "SP.Taxonomy.js", ExecuteTaxonomyFunctions);
});
}
);
});
function ExecuteTaxonomyFunctions()
{
CreateTaxonomyGroup();
CreateTermSet();
CreateTerms();
ReadTerms();
}
function CreateTaxonomyGroup()
{
var context = SP.ClientContext.get_current();
//Get the taxonomy session object
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Get the default termstore for the site collection
var termStore =taxSession.getDefaultSiteCollectionTermStore();
//New group with new guid
var newGroup = termStore.createGroup("IT",groupGuid);
context.load(newGroup);
context.executeQueryAsync(function(){
alert("Group Created with Name : "+ newGroup.get_name());
},function(sender,args){
alert(args.get_message());
});
}
function CreateTermSet()
{
var context = SP.ClientContext.get_current();
//Get the taxonomy session object
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Get the default termstore for the site collection
var termStore =taxSession.getDefaultSiteCollectionTermStore();
//Get the term group just created by using GUID
var termGroup= termStore.getGroup(groupGuid);
//Create a termset with new GUID, 1033 is lcid for US English
var termSet= termGroup.createTermSet("Developers", termSetGuid, 1033);
context.load(termSet);
context.executeQueryAsync(function(){
alert("Term Set Created with Name : "+ termSet.get_name());
},function(sender,args){
alert(args.get_message());
});
}
function CreateTerms()
{
var context = SP.ClientContext.get_current();
//Get the taxonomy session object
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Get the default termstore for the site collection
var termStore =taxSession.getDefaultSiteCollectionTermStore();
//Get the term group just created by using GUID
var termGroup= termStore.getGroup(groupGuid);
//Get the termset by GUID
var termSet=termStore.getTermSet(termSetGuid);
//Create term with new GUID
var term1= termSet.createTerm("Developer", 1033, "1c88007a-b6f0-4175-b91b-ac02f95376d9");
var term2= termSet.createTerm("UI Developer", 1033, "676186d9-434e-4998-a050-18043ab66d08");
context.load(term1);
context.load(term2);
context.executeQueryAsync(function(){
alert("Term Created with Names : "+ term1.get_name()+ "," + term2.get_name());
},function(sender,args){
alert(args.get_message());
});
}
function ReadTerms()
{
var context = SP.ClientContext.get_current();
//Get the taxonomy session object
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Get the default termstore for the site collection
var termStore =taxSession.getDefaultSiteCollectionTermStore();
//Get the term group just created by using GUID
var termGroup= termStore.getGroup(groupGuid);
//Get the termset by GUID
var termSet=termStore.getTermSet(termSetGuid);
var terms= termSet.getAllTerms();
context.load(termGroup);
context.load(termSet);
context.load(terms);
context.executeQueryAsync(function(){
var result = "Group:"+ termGroup.get_name()+" \n";
result+= "Term Set:"+ termSet.get_name()+" \n";
var termEnumerator = terms.getEnumerator();
result+= "Terms: \n";
while(termEnumerator.moveNext()){
var currentTerm = termEnumerator.get_current();
result+= currentTerm.get_name() + "\n";
}
alert(result);
},function(sender,args){
alert(args.get_message());
});
}
Below is the result snapshot of terms created.
I added a another post which shows you how to add item in list which contain metadata column using javascript object model.