How to query SP2013 Managed metadata term store with JSOM
This post will show how to read the Taxonomy Term Store with JSOM.
Taxonomy Term store can be accessed in a SharePoint App, with permission scope of Taxonomy.
To be able to use the JSOM API for Taxonomy you need to reference SP.Taxonomy.js found in _layouts/15/ the best way to reference this would be in the document ready.
Taxonomy Term store can be accessed in a SharePoint App, with permission scope of Taxonomy.
To be able to use the JSOM API for Taxonomy you need to reference SP.Taxonomy.js found in _layouts/15/ the best way to reference this would be in the document ready.
$(document).ready(function () {
var scripRef = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scripRef+ "SP.Runtime.js",
function () {
$.getScript(scripRef+ "SP.js", function () {
$.getScript(scripRef+ "SP.Taxonomy.js", function () {
context = SP.ClientContext.get_current();
//Call your code here.
getTermStores();
});});});})
Read from Taxonomy
function getTermStores() {
session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = session.get_termStores();
context.load(session);
context.load(termStores);
context.executeQueryAsync(
function(){
termStoreEnum = termStores.getEnumerator();
var termStores = "TermStores: /n";
while (termStoreEnum.moveNext()) {
var currentTermStore = termStoreEnum.get_current();
var termStoreID = currentTermStore.get_id();
var termStoreName = currentTermStore.get_name();
termStores += "Name: " + termStoreName + " ID:" + termStoreID;
}
}, function(){
//failure loading.
});
}
This guide on querying the SP2013 Managed Metadata Term Store with JSOM is incredibly useful! Just like Razor host simplifies web hosting, mastering this query can streamline your SharePoint development.
ReplyDelete