如何从在线商店SharePoint在线检索术语

时间:2016-09-08 10:18:17

标签: javascript sharepoint sharepoint-online sharepoint-jsom

我有一个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]

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

1 个答案:

答案 0 :(得分:1)

如果托管元数据字段允许多个值,则该对象的类型为 TaxonomyFieldValueCollection ;否则它将是一个 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
}