MOSS 2007 : WSS 3.0 : How do you add a new Site Column to a Content Type using the MOSS object model?
How do you add a new Site Column to a Content Type using the MOSS object model?
Here is the sample code
SPWeb web = new SPSite("http://localhost:4040").OpenWeb();
SPContentType myCT = web.ContentTypes["myNewContentType"];
The following MSDN article speaks about “Updating Child Content types”.
The example given in the article uses the SPFieldCollection object (SPContentType.Fields) to add columns to the content type. But when we use the sample given in the article we end up with the error message “SPException: This functionality is unavailable for field collections not associated with a list.”. I found that the columns are not added into content type and that are referenced in content type from the following article.
Also the above article “How to: Reference a Column in a Content Type” says,
The Fields property returns an SPFieldCollection object. Each SPField in this collection represents a "merged view" of the base column definition and any overwritten properties specified in the column reference in the content type. Because of this, you cannot add columns directly to this collection. Attempting to do so results in an error.