Friday, April 19, 2013

SPFieldUrl adding to contenttype

This code snippet may be simple but I spent couple of hours to get things done.On my searching I learned the difference between SPFieldUrlValue,SPFieldType.URL and SPFieldLink. Simply,I wanted to add the "HyperLink" column to my existing ContentType.
                SPWeb web = SPContext.Current.Web;
                SPContentTypeId cId = new SPContentTypeId("0x0100AF98B284ECB44322916F1A1AD1A9E31C");
                SPContentType cType = new SPContentType(cId, web.ContentTypes, "MurugesanCType");
                web.Fields.Add("T2", SPFieldType.URL, true);
                SPField f = web.Fields["T2"];
                f.Description = "Type your Org NAme";
                web.ContentTypes[cId].FieldLinks.Add(new SPFieldLink(f));
                web.ContentTypes[cId].Update();