Saturday, January 28, 2012

Sharepoint - Field manipulation in sharepoint list programmatically

Add column to sharepoint list



using (SPSite siteCollection = new SPSite(url))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                     SPList list = web.Lists[“ListName”];
                      list.Fields.Add("fieldName", SPFieldType.Boolean, true);
                     list.Update();

   
                    }
                }


Delete column from list


using (SPSite siteCollection = new SPSite(url))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                     SPList list = web.Lists[“ListName”];
                      list.Fields[“fieldname”].Delete();
                     list.Update();

   
                    }
                }



Add column from site column list


using (SPSite siteCollection = new SPSite(url))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                     SPList list = web.Lists[“ListName”];
                      list.Fields.Add(siteCollection.RootWeb.Fields[fieldName]);
                                    list.Update();

   
                    }
                }
           








           

No comments:

Post a Comment