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("https://localhost:4040").OpenWeb();

   SPContentType myCT = web.ContentTypes["myNewContentType"];

   myCT.FieldLinks.Add(new SPFieldLink(web.Fields["abc"]));

   myCT.Update();

 

Note :

The following MSDN article speaks about “Updating Child Content types”.

https://msdn2.microsoft.com/en-us/library/ms442695.aspx

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.

https://msdn2.microsoft.com/en-us/library/aa543526.aspx

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.

Keep Coding...