ysfibm ysfibm - 6 months ago 38
Javascript Question

How to retrieve term from term store SharePoint online

I have a SharePoint list and I'm retrieving metadata from my list using JavaScript, but I have one issue: how to retrieve metadata of a field that contains terms from the term store?

Here's is my code:

function getmetadata()
var listGuid = GetUrlKeyValue("List");
var itemId = GetUrlKeyValue("ID");
var clientContext = new SP.ClientContext();
var item = clientContext.get_web().get_lists().getById(listGuid).getItemById(itemId);
var logos = item.get_item("logo2");
var code = item.get_item("Code_postal_x002d_batiment");
var Surface = item.get_item("Surface");
var complement = item.get_item("Secteur_activite_x002d_complement");
var porteur = item.get_item("Nom_client");
var mission = item.get_item("Mission_x002d_Artdesk");
var vedette = item.get_item("photo_x0020_vedette");
document.getElementById("logo").innerHTML = logos;
document.getElementById("codepostal").innerHTML = code;
document.getElementById("surface").innerHTML = Surface;
document.getElementById("complement").innerHTML = complement;
document.getElementById("porteur").innerHTML = porteur;
document.getElementById("mission").innerHTML = mission;
document.getElementById("photovedette").innerHTML = vedette;


For example this is a term from term store: "Mission_x002d_Artdesk", but the value returned is:
[object Object]

How can I retrieve the term value from term store?


If the managed metadata field allows multiple values, the object will be of type TaxonomyFieldValueCollection; otherwise it'll be a single TaxonomyFieldValue.

You can grab the Label of single managed metadata field values, but not on collections; you'd need to loop through a collection to grab the Label on each individual value in the collection.

Getting the label of a single-value Taxonomy Field

var fieldValue = item.get_item("FieldName");
var fieldLabel = fieldValue.Label;

Getting the label(s) of a multi-value Taxonomy Field

var fieldValue = item.get_item("FieldName");
var taxEnumerator = fieldValue.getEnumerator();
    var currentTerm = taxEnumerator.get_current();
    var label = currentTerm.Label;
    // do something with the label here