通过TermName获取GUID - jsom

时间:2015-03-13 05:53:04

标签: javascript jquery sharepoint-online

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

作为一个例子,如果我通过“我的第1项”,它应该返回相应的“我的第1项”的GUID。

enter image description here

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

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

1 个答案:

答案 0 :(得分:0)

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

以下示例演示了如何使用JSOM通过Label查找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());
     });

});
相关问题