in

add custom fields to a list

   create a new list using a blank template

-          programmatically add the custom fields to the new list

-          Update the list to commit the addition of the new fields

-          Create a new view based on the default “All Items” view

-          Add the same custom fields to the new view

-          Delete the original view

-          Make the custom view as the new default view

 

Code Snippet

 

SPSite site = new SPSite("http://localhost/sites/Sergey/default.aspx");

       SPWeb web = site.OpenWeb();

 

       SPListCollection coll = web.Lists;

       SPListTemplateCollection tmplcoll = web.ListTemplates;

 

       SPFieldCollection fieldcoll;

       SPListTemplate temp = tmplcoll[0];

       Guid gd = coll.Add("NewList9","NewList9",temp);

       coll[gd].Fields.Add("Test1",SPFieldType.Text,false);

       coll[gd].Fields.Add("Test2",SPFieldType.Text,false);

       coll[gd].Update();

                                               

       string defaultquery = coll[gd].Views[0].Query;

       SPViewCollection viewcoll = coll[gd].Views;

       Guid anothergd = coll[gd].Views[0].ID;

       viewcoll.Delete(anothergd);

                                                

       System.Collections.Specialized.StringCollection viewfields = new System.Collections.Specialized.StringCollection();

       viewfields.Add("Title");

       viewfields.Add("Test1");

       viewfields.Add("Test2");

                                               

       coll[gd].Views.Add("All Items",viewfields,defaultquery,100,true,true);

       coll[gd].Update();


 

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback url for this post:
http://snowcounty.net/blogs/rliang/trackback.ashx?PostID=346

» Comments

    No comments yet. Be the first to comment!