我的应用程序读取SharePoint术语库,需要获取与用户语言关联的标签。我得到了用户的语言和lcid,然后我使用以下代码阅读了分类中某个节点下的所有术语:
... some code to get the Term Store, then Term Group, then Term Set, and finally startTerm
var tsTerms = startTerm.get_terms();
context.load(tsTerms);
context.executeQueryAsync(
function () {
var termsEnum = tsTerms.getEnumerator();
while (termsEnum.moveNext()) {
var currentTerm = termsEnum.get_current();
var termName = currentTerm.get_name();
var userLabel = currentTerm.getDefaultLabel(lcid);
var userLabelValue = userLabel.get_value();
console.log ("Label=", userLabel, userLabelValue)
... more code ...
在while循环中,除了标签之外,我可以获得我需要的术语的所有属性。在我在网上找到的其他示例中,为了获取默认标签,我的userLabel对象将在上下文中加载,然后调用另一个context.executeQueryAsync。所有这些都是有道理的,但这会导致大量调用SharePoint服务器。
但是,当我向控制台写入userLabel对象时,显示为SP.Result类型,当我打开它时,我在m_value下面看到了我想要的标签。因此,不需要再次访问服务器。但是,userLabelValue返回为0 - 显然,get_value()不起作用。在MSDN文档中,SP.Result对象类型仅供内部使用。有没有办法提取它存储的数据?
我附上了扩展对象的控制台图片,我们清楚地看到了m_value =" Contrat",这是我需要的标签。
答案 0 :(得分:1)
根据LCID
使用SP.Taxonomy.Term.getDefaultLabel Method获取此字词的默认Label:
function getTermDefaultValue(termId,lcid,success,failure)
{
var context = SP.ClientContext.get_current();
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termDefaultValue = taxSession.getTerm(termId).getDefaultLabel(lcid);
context.executeQueryAsync(function() {
success(termDefaultValue);
},
failure);
}
注意:
SP.Taxonomy.Term.getDefaultLabel
方法需要locale identifier (LCID)作为标签。
<强>用法强>
var layoutsRoot = _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/';
$.getScript(layoutsRoot + 'sp.taxonomy.js',
function () {
var termId = 'dff82ab5-6b7a-4406-9d20-40a8973967dd';
getTermDefaultValue(termId,1033,printLabelInfo,printError);
});
function printLabelInfo(label)
{
console.log(String.format('Default Label: {0}',label.get_value()));
}
function printError(sender,args){
console.log(args.get_message());
}
答案 1 :(得分:0)
我遇到了同样的问题并找到了解决方案。而不是使用getDefaultLabel(lcid),请使用:
<强> termSet.getTerm(Termid).getAllLabels(LCID).itemAt(0).get_value(); 强>
在我看来,这和'getDefaultLabel'一样,但它有效。它可能会导致比其他功能更多的负载,但这个适用于我