Enterprise Keywords management in Office 365 via CSOM

Overview

SharePoint 2013 has introduced Microsoft.SharePoint.Client.Taxonomy namespace for metadata management via CSOM. Since Taxonomy API also allows to manage Keywords, i would like to demonstrate how to get/set Enterprise Keywords field values in this post.

Adding Enterprise Keywords column into List

The first example demonstrates how to add Enterprise Keywords column into List:

Setting  Enterprise Keywords field value

The below example demonstrates how to set Enterprise Keywords field value:

Key points:

  • 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 is intended for.  After Keyword has been retrieved or created if it not existed, the value of Enterprise Keyword field is set (TaxonomyFieldValueCollection type).
Usage

Results

DocumentsWithTaxKeyword TermStore_Keywords

Advertisements

7 thoughts on “Enterprise Keywords management in Office 365 via CSOM

  1. Good post! Thank you. Sometimes the SharePoint API is so awkward… Do you know how I can extract a text representation of the taxonomyfieldvaluecollection? I want to copy values from one item to another. To “re-assemble” the delimited values seems a bad approach.

    • Thanks! Regarding converting taxonomyfieldvaluecollection into text representation, indeed, it seems there is no straightforward way.. so, i’m also not aware any other ways except parsing taxonomyfieldvaluecollection object

  2. Thanks for this! I find it is working well but sometimes I get the error “There is already a term with the same default label and parent term”. This seems to happen with terms that have already been added to the Keywords term store. However, a lot of the terms have already been added and the KeywordsManager works well.

    Have you experienced this issue before? Any guidance on your findings?

    Thanks!

  3. Vadim, please disregard my last comment. I figured out most of the issues. Most have to do with content and the fact that terms are Case Sensitive. For example, the following line of code will not find a match if the case of the terms you wish to add and the case of the terms in the store are different (for example: Blogs and blogs):

    var result = ctx.LoadQuery(keywords.Where(k => k.Name == name));

    However, when the code tries to create the term (since a match was not found) it throws an exception about entering a duplicate term. So creating a term seems to be case insensitive.

    To circumvent this I have to load all the keywords and then perform another LINQ query with string comparison that is OrdinalCaseIgnore. Not very efficient but I get errors when trying to specify the case comparison at LoadQuery time. (k => k.Name.Equals(name, StringComparison.OrdinalCaseIgnore)) throws an error.

    Additionally, I found that you need to encode the ampersand, speechmarks, and trim any white space around the term you are trying to add. There is a NormalizeName method of the Term class that helps with this. However, it requires a round trip to the server for each term so I built a custom method.

    Cheers! Thanks again for a helpful blog post!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s