Associate a site column to a content type using SharePoint 2007 Object Model

I recently came across a question whether a site column can be associated with a content type using SharePoint 2007 object model? I thought it should be fairly simple. However, it was really a big deal of research. Fortunately, our former buddy, Karthick had this documented in his blog already. See here. I have provided the code snippet for quick reference below:

SPSite site = null;

SPWeb web = null;

SPContentType cntType = null;

try

{

site = new SPSite("https://<sharepointserverurl>");

      web = site.OpenWeb();

      cntType = web.ContentTypes["testCntType"];//lnkCntType

      cntType.FieldLinks.Add(new SPFieldLink(web.Fields["lnkCntType"]));

      cntType.Update();

      Console.WriteLine("Success");

}

catch (Exception e)

{

Console.WriteLine("Exception :: " + e.Message);

}

finally

{

if (web != null)

web.Close();

if (site != null)

site.Close();

}