zoukankan      html  css  js  c++  java
  • Working With Taxonomy Field in CSOM

    How to create taxonomy field with CSOM

    If you need to programmatic create a taxonomy field, you need to:

    1. Know the termstore id and termset id that associated with this column
    2. Create a taxonomy field with schema include some basic attributes
    3. Update some essential attributes for the taxonomy field
                //Add field as xml with some basic attribute
                XElement fieldSchema = new XElement("Field");
                fieldSchema.SetAttributeValue("Type", "TaxonomyFieldType");
                fieldSchema.SetAttributeValue("DisplayName", "TestField");
    
                list.Fields.AddFieldAsXml(fieldSchema.ToString(), true, AddFieldOptions.DefaultValue);
    
                context.ExecuteQuery();
    
                //get taxonomy field
                context.Load(list.Fields, fields => fields.IncludeWithDefaultProperties().Where(f => f.Title == "TestField"));
                context.ExecuteQuery();
    
                var taxonomyField = list.Fields[0] as TaxonomyField;
    
                //Update essential attributes for taxonomy field
    
                // Specify the Id of termset associated with the column
                taxonomyField.TermSetId = new Guid("a518b741-e2eb-429a-8dc7-f737945b25a3");
                // Id of the term store 
                taxonomyField.SspId = new Guid("151320eb-50d2-468d-b42e-1531301953a7");
    
                taxonomyField.Update();
    
                context.ExecuteQuery();

    How to update taxonomy field value with CSOM

    If you need to programmatic update taxonomy field value, you need to get the term id and label first. This sample shows how to update taxonomy field with multiple values.

                ListItem item = list.GetItemById(1);
    
                TaxonomyFieldValueCollection col = new TaxonomyFieldValueCollection(context, "", taxonomyField);
                col.PopulateFromLabelGuidPairs("invrev-mel|44c3035c-b84b-4da3-b7fc-1097ab5e07e2;BEE|c0e6e20a-142f-4f07-ac10-3cf1aa692b13;05 Industry Data and News|b4030964-270b-4f80-bedd-0c2d9d7fae22");
    
                taxonomyField.SetFieldValueByValueCollection(item, col);
  • 相关阅读:
    为什么常见编程语言中数组的索引都是从0开始?
    【转载】计算机经典论文选读
    Jetbrains-别人家的公司
    2019.9沉淀反思
    HttpUrlConnection流传输问题(正确传输包含中文的JSON字符串)
    记一次SpringBoot启动缓慢问题的解决过程
    Maven发布jar包到私库
    2016年回顾
    Java集合框架源码分析之ArrayList
    关于哔哩哔哩直播姬
  • 原文地址:https://www.cnblogs.com/myprogram/p/4593987.html
Copyright © 2011-2022 走看看