【问题标题】:Reading SharePoint Taxonomy Term Store and getDefaultLabel(lcid)阅读 SharePoint 分类术语库和 getDefaultLabel(lcid)
【发布时间】:2015-01-23 22:06:03
【问题描述】:

我的应用程序读取 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循环中,我可以得到我需要的term的所有属性,除了label。在我在网上找到的其他示例中,为了获取默认标签,我的 userLabel 对象将被加载到上下文中,然后调用另一个 context.executeQueryAsync。这一切都说得通,但这会导致对 SharePoint 服务器的大量调用。

但是,当我向控制台写入 userLabel 对象时,它显示为 SP.Result 类型,当我打开它时,我会在 m_value 下看到我想要的标签。所以应该不需要再去服务器了。但是,userLabelValue 返回为 0 - 显然, get_value() 不起作用。在 MSDN 文档中,SP.Result 对象类型仅供内部使用。有没有办法提取它存储的数据?

我附上了一张扩展对象的控制台图片,我们清楚地看到 m_value = "Contrat",这是我需要到达的标签。

【问题讨论】:

    标签: javascript sharepoint taxonomy csom


    【解决方案1】:

    使用SP.Taxonomy.Term.getDefaultLabel Method 根据LCID 获取此术语的默认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());
    }    
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题并找到了解决方案。不要使用 getDefaultLabel(lcid),而是使用这个:

      termSet.getTerm(Termid).getAllLabels(lcid).itemAt(0).get_value();

      在我看来,这与“getDefaultLabel”的作用相同,但它有效。它可能会比其他功能造成更多的负载,但这个对我有用

      【讨论】:

        猜你喜欢
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-31
        • 2010-12-29
        • 1970-01-01
        相关资源
        最近更新 更多