使用C#CSOM更新/更改sharepoint中文档库列表中的自定义TaxonomyFieldValue

时间:2018-04-23 10:54:43

标签: sharepoint sharepoint-2013 custom-taxonomy sharepointdocumentlibrary taxonomy-terms

包含名为subject的自定义分类字段列的文档列表。 需要更新数千条记录/文件的主题。 请任何想法使用C#CSOM

以程序方式更新分类标准字段

2 个答案:

答案 0 :(得分:0)

请尝试使用此方法:

public void UpdateTaxonomyField(ClientContext ctx, List list,ListItem listItem,string fieldName,string fieldValue)
        {
            Field field = list.Fields.GetByInternalNameOrTitle(fieldName);
            TaxonomyField txField = clientContext.CastTo<TaxonomyField>(field);
            TaxonomyFieldValue termValue = new TaxonomyFieldValue();
            string[] term = fieldValue.Split('|');
            termValue.Label = term[0];
            termValue.TermGuid = term[1];
            termValue.WssId = -1;
            txField.SetFieldValueByValue(listItem, termValue);
            listItem.Update();
            ctx.Load(listItem);
            ctx.ExecuteQuery();
        }

答案 1 :(得分:0)

   public static void UpdateListofLibraryHavingTaxonomyField()
    {
        string siteUrl = "http://abc:55555/sites/xyz/";
        string libName = "Knowledge Repos";
        string strTermGuid = "Your new/update term guid";
        try
        {
            Dictionary<string, string> dictIdsSubjectsChange = ReadFromExcel();//Here to read all records (approx 2000 records) that we want to change
            ClientContext clientContext = new ClientContext(siteUrl);
            List list = clientContext.Web.Lists.GetByTitle(libName);
            FieldCollection fields = list.Fields;
            Field field = fields.GetByInternalNameOrTitle("Subject1");
            ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
            clientContext.Load(listItems, items => items.Include(i => i["Subject1"], i => i["ID"]));
            clientContext.Load(fields);
            clientContext.Load(field);
            clientContext.ExecuteQuery();
            TaxonomyField txField = clientContext.CastTo<TaxonomyField>(field);               
            TaxonomyFieldValue termValue = null;
            if (dictIdsSubjectsChange != null)
            {
                foreach (ListItem listItem in listItems)//Loop through all items of the document library 
                {
                    string strCurrentID = "0";
                    try
                    {
                        strCurrentID = listItem["ID"].ToString();
                    }
                    catch (Exception) { }                      
                    if (dictIdsSubjectsChange.ContainsKey(strCurrentID))//Checking to change ot not
                    {
                        termValue = new TaxonomyFieldValue();
                        termValue.Label = "Special Knowledge";
                        termValue.TermGuid = strTermGuid;
                        termValue.WssId = 246;
                        txField.SetFieldValueByValue(listItem, termValue);
                        listItem.Update();
                        clientContext.Load(listItem);
                    }
                }
                clientContext.ExecuteQuery();
            }
        }
        catch (Exception ex) { }
    }
}