Issue


In a certain situation, an exception is thrown, "A Content Type Which Does Not Belong To A Collection Cannot be Updated", when adding columns to a content type programmatically.  Doing a walk through of the code reveals the problem; a new content type was instantiated, then some columns were added to it, when the exception is thrown.

Solution


The reason was that although the content type was instantiated it was not added to any collection (in other words it was not associated with any site). Add the following lines of code (which add the content type to a site collection) before calling the update method on the content type.

SPWeb web = site.OpenWeb("http://contorso/");
web.ContentTypes.Add(MyContentType);