SPWeb web = SPContext.Current.Web;
SPContentTypeId cId = new SPContentTypeId("0x0100AF98B284ECB44322916F1A1AD1A9E31C");
SPContentType cType = new SPContentType(cId, web.ContentTypes, "MurugesanCType");
web.ContentTypes.Add(cType);
Guid termSetId;
TaxonomySession session = new TaxonomySession(SPContext.Current.Site);
TermStore store = session.TermStores[0];
Group group = store.Groups[1];
TermSetCollection tCols = group.TermSets;
foreach (TermSet set in tCols)
{
if(set.Name.ToString().Equals("Role"))
{
termSetId = set.Id;
TaxonomyField tField = web.Fields.CreateNewField("TaxonomyFieldType", "Profile Role") as TaxonomyField;
tField.SspId = set.TermStore.Id;
tField.TermSetId = termSetId;
tField.TargetTemplate=string.Empty;
tField.AllowMultipleValues=false;
tField.CreateValuesInEditForm=true;
tField.Open=true;
tField.AnchorId=Guid.Empty;
tField.Group = "Meta Data - Custom";
web.Fields.Add(tField);
SPField spField = web.Fields["Profile Role"];
web.ContentTypes[cId].FieldLinks.Add(new SPFieldLink(spField));
web.ContentTypes[cId].Update();
}
}
Web.Update();