Add Properties or Delete List Folder Content Type

        private void button1_Click(object sender, EventArgs e)

        {

            string webURL = "http://richsharepoint:90/ApplicationManagement";

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                using (SPSite site =new SPSite (webURL))

                {

                    using (SPWeb web = site.OpenWeb("ApplicationManagement"))

                    {

                        SPListCollection lists=web.Lists;

                        SPList list = null;

                        for (int i = 0; i < lists.Count; i++)

                        {

                            list = lists[i];

                            if (IsNeedList(list.Title))

                            {

                                ExtendFolderPerproty(web, list);

                            }

                        }

                    }

                }

            });

        }



        private bool IsNeedList(string listName)

        {

            string[] listNames = new string[] { "PictureLibraryAnson"};

            for (int i = 0; i < listNames.Length; i++)

            {

                if (listNames[i].ToString()==listName)

                {

                    return true;

                }

            }

            return false;

        }



        private void ExtendFolderPerproty(SPWeb web, SPList list)

        {

            SPContentType ct = list.ContentTypes["Folder"];

            ct.Sealed = false;

            //remove the properties

            ct.FieldLinks.Delete("NumberColumn");

            ct.FieldLinks.Delete("Direct_x0020_Link");

            ct.FieldLinks.Delete("Media_x0020_Category");

            ct.FieldLinks.Delete("Market_x0020_Category");

            ct.Update();

           //web.Fields.Add("NumberColumn", SPFieldType.Number,false);

            //SPFieldLink fLink1 = new SPFieldLink(web.Fields["NumberColumn"]);

            //ct.FieldLinks.Add(fLink1);



            //add Direct Link column

            string hColumnName = "Direct Link";

            web.Fields.Add(hColumnName, SPFieldType.URL, false);

            SPFieldLink hLink = new SPFieldLink(web.Fields[hColumnName]);

            ct.FieldLinks.Add(hLink);



            //add Media Category column

            MediaChoiceColumn(web, ct);

           ct.Update();

        }



        private void MediaChoiceColumn(SPWeb web, SPContentType ct)

        {

            string columnName = "Media Category";

            web.Fields.Add(columnName, SPFieldType.Choice, false);

            SPFieldChoice choicefield = (SPFieldChoice)web.Fields[columnName];

            // Add a group to the filed       

           // choicefield.Group = "MyGroup";

            // Add choices    

            string[] choiceItems = new string[] { "Bulletin","Junior Poster","Poster","Spectacular",

                "Wall","Wrapped Building/Poster","Mobile Billboard","Airport Media",

                "Bus Media","Rail/ Subway Media","Taxi Media","Mall Media",

                "Sports Media (Arena/Stadium)","Street Furniture","Place-Based Media (not Digital)",

                "Alternative/Promotional Media","Guerilla Media","Street Teams","Experiential / Event",

                "Digital Billboard","Digital Spectacular","Digital Poster","Digital Street Furniture",

                "Digital Airport Media","Digital Transit Media","Digital Mobile Media",

                "Digital Taxi Media","Digital Projection Media","Digital Place Based - Medical & Fitness",

                "Digital Place Based - Education & Office","Digital Place Based - Leisure",

                "Digital Place Based - Retail", "Digital Alternative Media","Mobile Media"

            };

            for (int j = 0; j < choiceItems.Length; j++)

            {

                //add choice

                choicefield.Choices.Add(choiceItems[j].ToString());

            }

            choicefield.EditFormat = SPChoiceFormatType.Dropdown;

            // Set the default choice        

            choicefield.DefaultValue = "Bulletin";

            choicefield.Update();

            SPFieldLink fl= new SPFieldLink(choicefield);

            ct.FieldLinks.Add(fl); 



        }



       //Delete list folder content type

       private void button2_Click(object sender, EventArgs e)

        {

            string webURL = "http://shengvm77:84" ;

            try

            {

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    using (SPSite site = new SPSite(webURL))

                    {

                        using (SPWeb web = site.OpenWeb("HomeAnson"))

                        {

                            SPListCollection lists = web.Lists;

                            SPList list = null;

                            for (int i = 0; i < lists.Count; i++)

                            {

                                list = lists[i];

                                if (list.Title == "PicAnson")

                                {

                                    SPContentType ct = list.ContentTypes["Folder"];

                                    list.ContentTypes.Delete(ct.Id);

                                    list.Update();

                                    break;

                                }

                            }

                        }

                    }

                });

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            

        }

 

你可能感兴趣的:(properties)