【问题标题】:How to add a term to TermCollection (taxonomy field)如何将术语添加到 TermCollection(分类字段)
【发布时间】:2011-11-30 16:37:18
【问题描述】:

在 sharePoint 2010 中,我想设置文档字段的分类值。该字段可以采用多个分类术语。

我做错了,因为taxoTerms.Concat(terms)TermCollection 类中的演员阵容失败:

    TaxonomyField taxoField = file.Item.Fields.GetFieldByInternalName(entry.Key) 
              as TaxonomyField;
    
    TaxonomySession taxoSession = new TaxonomySession(web.Site);

    TermStore store = taxoSession.TermStores[taxoField.SspId];

    TermSet termSet = store.GetTermSet(taxoField.TermSetId);

    if (taxoField.AllowMultipleValues)
    {   
        string[] taxoValues = entry.Value.Split(';');

        TermCollection taxoTerms = termSet.GetTerms(taxoValues[0], true);
                                            
        for (int j = 1; j < taxoValues.Length; j++)
        {
            TermCollection terms = termSet.GetTerms(taxoValues[j], true);

            if (terms.Count > 0)
            {
                taxoTerms = (TermCollection)taxoTerms.Concat(terms);
            }
        }

        taxoField.SetFieldValue(file.Item, taxoTerms); 
    }

您知道如何将术语添加到我的 TermCollection 对象中,以便将术语值保存在字段中吗?

【问题讨论】:

    标签: sharepoint taxonomy


    【解决方案1】:

    我找到了我的解决方案。这里是:

    TaxonomyField taxoField =
        file.Item.Fields.GetFieldByInternalName(entry.Key) as TaxonomyField;
    
    TaxonomySession taxoSession = new TaxonomySession(web.Site);
    
    TermStore store = taxoSession.TermStores[taxoField.SspId];
    
    TermSet termSet = store.GetTermSet(taxoField.TermSetId);
    
    if (taxoField.AllowMultipleValues)
    {   
        string[] taxoValues = entry.Value.Split(';');
    
        TermCollection terms = termSet.GetAllTerms();
    
        List<string> taxonomyValueList = taxoValues.ToList<string>();
    
        TaxonomyFieldValueCollection fieldValues = new TaxonomyFieldValueCollection(taxoField);
    
        foreach (Term term in terms)
        {
            if (taxonomyValueList.Contains(term.Name))
            {
                TaxonomyFieldValue fieldValue = new TaxonomyFieldValue(taxoField);
    
                fieldValue.TermGuid = term.Id.ToString();
                fieldValue.Label = term.Name;
                fieldValues.Add(fieldValue);
            }
        }
    
        taxoField.SetFieldValue(file.Item, fieldValues);
    } 
    

    希望对他人有所帮助。

    【讨论】:

      【解决方案2】:

      这是一个可行的示例:

      var item = file.Item;
      var taxonomyField = item.Fields.GetFieldByInternalName(entry.Key);
      var values = new TaxonomyFieldValueCollection(taxonomyField);
      values.PopulateFromLabelGuidPairs(entry.Value);
      item[entry.Key] = values;
      item.Update();
      

      我没有在生命系统上测试它,所以可能会有一些额外的工作,但我希望你能得到大致的想法。 entry.Value 字符串中的值必须包含 |和 ;分隔的标签列表。如果标签不存在,您必须先创建它并获取其 id,然后再将其保存到项目中。

      HTH 沃伊塔

      【讨论】:

      • 感谢您的提议。我添加了另一种方法来完成这项工作。
      猜你喜欢
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2018-11-04
      • 2014-08-16
      相关资源
      最近更新 更多