Tuesday, March 05, 2013

Site Column creating via code

Site columns can be created while site provisioning and later these field can be attached to content type and associating this content type to a specific list. Place below code in "Feature Receiver".
 public string _firstNameText = "";
        public string _secondNameText = "";    

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            
            using (SPSite site = (SPSite)properties.Feature.Parent)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.Fields.AddFieldAsXml(_firstNameText);
                    web.Fields.AddFieldAsXml(_secondNameText);
                    
                    web.Update();
                }
            }

        }