如何从术语库 SharePoint 在线检索术语

How to retrieve term from term store SharePoint online

我有一个 SharePoint 列表,我正在使用 JavaScript 从我的列表中检索元数据,但我有一个问题:如何从术语库中检索包含术语的字段的元数据?

这是我的代码:

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);
    clientContext.load(item);
    clientContext.executeQueryAsync(
        function(){
             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;

        },
        function(sender,args){
            alert(args.get_message());
        });
}

例如,这是术语库中的术语:"Mission_x002d_Artdesk",但返回的值为:[object Object]

如何从术语库中检索术语值?

如果托管元数据字段允许多个值,则对象的类型将为 TaxonomyFieldValueCollection; otherwise it'll be a single TaxonomyFieldValue

您可以获取单个托管元数据字段值的 Label,但不能获取集合;您需要遍历一个集合以获取集合中每个单独值的 Label

获取单值分类字段的标签

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

获取多值分类字段的标签

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