I have included the enterprise keywords column in my contenttype and list and I try to set the value using the managed CSOM. I just used the default already existing column so the column name is "TaxKeyword" and the hidden note field for that column is "TaxKeywordTaxHTField".

I tried to set the value like this:

item["TaxKeywordTaxHTField"] = "-1;#Test|6138f1b1-b85a-4149-b06f-f79f6069a33d";

This works completely fine for most types of files (aspx, txt, pdf) but doesn't work for all office file types (docx, xlsx, pptx). I don't get any error when I call the executequery method but the value is just not saved. When I update the value in the UI, everything is working fine.

How can I set the enterprise keywords value programatically for office documents using managed CSOM?

The operation of setting Enterprise Keywords field value consists of two steps:

  • first one to resolve Keyword in Managed Metadata service application (MMS), this what EnsureKeyword method from below example is intended for
  • After Keyword has been retrieved or created if it not existed, the value of Enterprise Keyword field is set to TaxonomyFieldValueCollection

The method KeywordsManager.SetTaxKeywordValue demonstrates how to set Enterprise Keywords field value:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;

namespace SharePoint.Client.Taxonomy
{
    /// <summary>
    /// Enterpise Keyword Manager
    /// </summary>
    public class KeywordsManager
    {

        /// <summary>
        /// Set Enterprise Keyword Value
        /// </summary>
        /// <param name="item">List Item</param>
        /// <param name="values">Keyword values</param>
        public static void SetTaxKeywordValue(ListItem item,string[] values)
        {
            var ctx = item.Context;
            var list = item.ParentList;
            var field = list.Fields.GetByInternalNameOrTitle(TaxKeywordFieldName);
            var taxKeywordField = ctx.CastTo<TaxonomyField>(field);
            ctx.Load(item,i=>i[TaxKeywordFieldName]);
            var keywords = values.Select(value => EnsureKeyword(taxKeywordField, value)).ToList();
            taxKeywordField.SetFieldValueByValueCollection(item, new TaxonomyFieldValueCollection(ctx, GetTermsString(keywords), taxKeywordField));    
        }


        /// <summary>
        /// Ensure Keyword 
        /// </summary>
        /// <param name="taxField"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private static Term EnsureKeyword(TaxonomyField taxField, string name)
        {
            var ctx = taxField.Context;
            var taxSession = TaxonomySession.GetTaxonomySession(ctx);
            var termStore = taxSession.GetDefaultKeywordsTermStore();
            var keywords = termStore.KeywordsTermSet.GetAllTerms();
            var result = ctx.LoadQuery(keywords.Where(k => k.Name == name));
            ctx.ExecuteQuery();
            var keyword = result.FirstOrDefault();
            if (keyword != null)
            {
                return keyword;
            }
            keyword = termStore.KeywordsTermSet.CreateTerm(name, DefaultLanguage, Guid.NewGuid());
            ctx.Load(keyword);
            ctx.ExecuteQuery();
            return keyword;
        }

        /// <summary>
        /// Retrieve formatted Term string
        /// </summary>
        /// <param name="term"></param>
        /// <returns></returns>
        private static string GetTermString(Term term)
        {
            return string.Format("-1;#{0}{1}{2}", term.Name, TaxonomyGuidLabelDelimiter,term.Id);
        }

        private static string GetTermsString(IEnumerable<Term> terms)
        {
            var termsString = terms.Select(GetTermString).ToList();
            return string.Join(";#", termsString);
        }



        private const string TaxKeywordFieldName = "TaxKeyword";

        private const int DefaultLanguage = 1033;

        private const string TaxonomyGuidLabelDelimiter = "|";
    }
}

Usage

using (var ctx = new ClientContext(webUri))
{
    var list = ctx.Web.Lists.GetByTitle(listTitle);
    var item = list.GetItemById(itemId);
    KeywordsManager.SetTaxKeywordValue(item,new []{"2013","2010"});
    item.Update();
    ctx.ExecuteQuery();
}

Results

enter image description here

enter image description here

For a more details please follow Enterprise Keywords management in Office 365 via CSOM post.

up vote 4 down vote accepted

I resolved the issue by setting the keywords in the following way:

 Microsoft.SharePoint.Client.Field entKeywordField = list.Fields.GetByInternalNameOrTitle("Enterprise Keywords");
 TaxonomyField entKeywordFieldTaxonomy = ctx.CastTo<TaxonomyField>(entKeywordField);
 entKeywordFieldTaxonomy.SetFieldValueByValueCollection(item, new TaxonomyFieldValueCollection(ctx, "-1;#Application|f897952a-107a-4988-9913-6d4094318ddb;#-1;#form|9f03d594-a4ef-443b-963d-1d05c3c85bd8", entKeywordField));

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.