【问题标题】:Get GUID By TermName - jsom通过 TermName 获取 GUID - jsom
【发布时间】:2015-05-15 13:13:10
【问题描述】:

我需要将术语名称作为参数传递并在 SharePoint 2013 JSOM 中返回该特定术语的 GUID

例如,如果我通过“我的学期 1”,它应该返回“我的学期 1”的相应 GUID。

var termGuid=getTermByName('My Term 1');

function getTermByName(TermName){
  return term.get_id();
}

【问题讨论】:

    标签: javascript jquery sharepoint-online


    【解决方案1】:

    SP.Taxonomy.TermStore.getTerms Method 可用于在TermStore 中查找Term

    以下示例演示如何使用 JSOM 按标签查找Term

    function findTermsByLabel(label,success,error)
    {
         var ctx = SP.ClientContext.get_current();
         var ts = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);
         var matchInfo = new SP.Taxonomy.LabelMatchInformation(ctx);
         matchInfo.set_termLabel(label);
         matchInfo.set_trimUnavailable(true);
         var termMatches = ts.getTerms(matchInfo);
         ctx.load(termMatches);
         ctx.executeQueryAsync(
            function(){
               var terms = termMatches.get_data();
               success(terms);
            },
            error);    
    }
    

    用法

    SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
    SP.SOD.registerSod('SP.Taxonomy.TaxonomySession', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
    SP.SOD.loadMultiple(['SP.ClientContext', 'SP.Taxonomy.TaxonomySession'], function(){
    
       var termLabel = '--term label goes here--';
    
       findTermsByLabel(termLabel,
         function(terms){
            if(terms.length == 0)
                console.log('Term has not been found');
            else if(terms.length > 1)    
                console.log(String.format("Several Terms exist with '{0}' label",termLabel));
            else {
                console.log(String.format('Term has been found: {0}'),terms[0].get_id().toString());     
            }    
         },
         function(sender,args){
            console.log(args.get_message());
         });
    
    });
    

    【讨论】:

    • 我试过这段代码,但它总是给出“未找到术语”.. 还不确定原因..(我已经正确给出了现有术语名称)
    猜你喜欢
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2014-11-23
    • 2012-08-06
    • 1970-01-01
    相关资源
    最近更新 更多