"This functionality is unavailable for fields not associated with a list"

I wanted to set the default termset node of a taxonomy field in a content type in a list.

(…the field should have different defaults depending on the type of library, but that's not important right now)

So. Easy. Get the list, get the content type, get the field, set the default. What could be easier?

var list = web.Lists.TryGetList("Documents"); // obvious "if"s removed

var contentType = list.ContentTypes["CT1"];

var field = contentType.Fields.GetFieldByInternalName("DocumentType") as TaxonomyField;

field.SspId = some guid

field.etc

field.Update(); // KABLAM

And that's when you receive the unfortunate error that is the subject of this tiny post.

Let us skip all the wrong attempts I made at solving this, and og straight to the solution: Do not get the field from the content type - get it directly from the list instead.

var list = web.Lists.TryGetList("Documents");

var field = list.Fields.GetFieldByInternalName("DocumentType") as TaxonomyField;

It does make sense. You can only change the field on a per list (or globally) scope; you can not do it per content type.

This article is part of the GWB Archives. Original Author: Norgean

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.