Cited: Working with Taxonomy and JavaScript in SharePoint 2013

Cited: SharePoint Adventures

A simple way to load all the 3 files is with the jQuery.getScript function:

$(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", execOperation); 
    });
  });
});

1) Query a particular Term Set and get all the Terms under it:

function execOperation(){
  //Current Context
  var context = SP.ClientContext.get_current();
  //Current Taxonomy Session
  var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
  //Term Stores
  var termStores = taxSession.get_termStores();
  //Name of the Term Store from which to get the Terms.
  var termStore = termStores.getByName("Metadata Service");
  //GUID of Term Set from which to get the Terms.
  var termSet = termStore.getTermSet("b49f64b3-4722-4336-9a5c-56c326b344d4");
  var terms = termSet.getAllTerms();
  context.load(terms);
  context.executeQueryAsync(function(){
    var termEnumerator = terms.getEnumerator();
    var termList = "Terms: \n";
    while(termEnumerator.moveNext()){
      var currentTerm = termEnumerator.get_current();
      termList += currentTerm.get_name() + "\n";
    }
    alert(termList);
  },function(sender,args){
    console.log(args.get_message());
  });
}

2) Query a particular Term Set and get a specific term under it:

function execOperation(){
  var context = SP.ClientContext.get_current();
  var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
  var termStores = taxSession.get_termStores();
  var termStore = termStores.getByName("Metadata Service");
  var termSet = termStore.getTermSet("b49f64b3-4722-4336-9a5c-56c326b344d4");
  // Guid of Term
  var term = termSet.getTerm("388b51fd-535b-445f-82ed-4b8067f9041b");
  context.load(term);
  context.executeQueryAsync(function(){
       alert(term.get_name());
  },function(sender,args){
    console.log(args.get_message());
  });
}

Using GUID to get getTerm is a simple and neat way in JSOM; however, the TermSet GUID is varied to each environment. 

To solved this issue, you just need to create Powershell script to set up custom Term Set with certain GUID to make it consistent everywhere. 

public TermSet CreateTermSet(
	string name,
	Guid newTermSetId,
	int lcid
)

Example: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.termstore.aspx

3) Create new Term Group under the Term Store:

function execOperation(){
  //Current Context
  var context = SP.ClientContext.get_current();
  //Current Taxonomy Session
  var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
  //Term Stores
  var termStores = taxSession.get_termStores();
  //Term Store under which to create the group.
  var termStore = termStores.getByName("Metadata Service");
  //New Group with name and new GUID
  var newGroup = termStore.createGroup("New Group Name","b300304a-1693-4629-a1c0-dff7bda644ff");

  context.load(newGroup);
  context.executeQueryAsync(function(){
    console.log(newGroup.get_name());
  },function(sender,args){
    console.log(args.get_message());
  });

}

4) Create new Term Set under a Term Group:

function execOperation(){
  //Current Context
  var context = SP.ClientContext.get_current();
  //Current Taxonomy Session
  var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
  //Term Stores
  var termStores = taxSession.get_termStores();
  //Term Store under which to create the Term Set.
  var termStore = termStores.getByName("Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==");
  //Get group by GUID
  var peopleGroup = termStore.getGroup("97eaa7b8-9778-4f61-acb3-7f47abba13c3");
  //Create New Term Set in Group with Name, New GUID and LCID
  var newTermSet = peopleGroup.createTermSet("New TermSet Name","49dac247-d315-4065-8718-e8c3f50e7dcd",1033);

  context.load(newTermSet);
  context.executeQueryAsync(function(){
    console.log(newTermSet.get_name());
  },function(sender,args){
    console.log(args.get_message());
  });

}

5) Create new Term under a Term Set:

function execOperation(){
  //Current Context
  var context = SP.ClientContext.get_current();
  //Current Taxonomy Session
  var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
  //Term Stores
  var termStores = taxSession.get_termStores();
  //Term Store under which to create the term.
  var termStore = termStores.getByName("Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==");
  //Term Set under which to create the term.
  var termSet = termStore.getTermSet("b49f64b3-4722-4336-9a5c-56c326b344d4");
  //Name of the term, LCID and a new GUID for the term.
  var newTerm = termSet.createTerm("India", 1033, "b49f64b3-4722-4336-9a5c-56c326b344a9");

  //newTerm.set_isAvailableForTagging(true);

  context.load(newTerm);
  context.executeQueryAsync(function(){
    alert("Term Created: " + newTerm.get_name());
  },function(sender,args){
    console.log(args.get_message());
  });

}

6) Get Value of a Single Value Taxonomy Column in a List:

function execOperation(){
  var context = SP.ClientContext.get_current();
  var list = context.get_web().get_lists().getByTitle('TaxonomyCustomList');
  var listItem = list.getItemById(1);
  context.load(listItem);
  context.executeQueryAsync(function(){
    //Single Value Taxonomy Column
    //Label
    var label = listItem.get_item("MyTaxColumn").get_label();
    //Term GUID
    var termGUID = listItem.get_item("MyTaxColumn").get_termGuid();
    //Type ID
    var typeID = listItem.get_item("MyTaxColumn").get_typeId();
    //WSS ID
    var wssID = listItem.get_item("MyTaxColumn").get_wssId();
  },function(sender,args){
    console.log(args.get_message());
});

}

7) Get Values of a Multi Value Taxonomy Column in a List:

function execOperation(){
  var context = SP.ClientContext.get_current();
  var list = context.get_web().get_lists().getByTitle('TaxonomyCustomList');
  var listItem = list.getItemById(1);
  context.load(listItem);
  context.executeQueryAsync(function(){
    //Multivalue Taxonomy Column
    var taxEnumerator = listItem.get_item("MyTaxColumn").getEnumerator();

    while(taxEnumerator.moveNext()){
      //Label
      var currentTerm = taxEnumerator.get_current();
      //Label
      var label = currentTerm.get_label();
      //Term GUID
      var termGUID = currentTerm.get_termGuid();
      //Type ID
      var typeID = currentTerm.get_typeId();
      //WSS ID
      var wssID = currentTerm.get_wssId();
    }	
  },function(sender,args){
    console.log(args.get_message());
  });

}
Advertisements

2 thoughts on “Cited: Working with Taxonomy and JavaScript in SharePoint 2013

  1. Neat. One question though – what about searching for particular terms in a termset? E.g. “All terms starting with ‘hel'”. I’ve tried your example using getTerms() rather than getAllTerms() and I can’t get it to work at all!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s