MOSS
Creating a List with a Custom View in MOSS
happynuri
2008. 4. 29. 17:03
The following code can be used to create a custom list in MOSS and apply a custom view to it:
// create list web.Lists.Add(listName, "", SPListTemplateType.GenericList); list = web.Lists[listName]; // Add your fields to the list list.Fields.Add("Field1", SPFieldType.Text, false); list.Fields.Add("Field2", SPFieldType.Text, false); // You can change the title of a field by accessing the SPField object SPField field1 = list.Fields["Field1"]; field1.Title = "Title Of Field 1"; field1.Update(); // You have to use a specialized string collection to add fields to a view System.Collections.Specialized.StringCollection strCol = new System.Collections.Specialized.StringCollection(); strCol = new System.Collections.Specialized.StringCollection(); strCol.Add("Field1"); strCol.Add("Field2"); // You can use a standard query here to only return certain data string query = string.Empty; query = ""; SPView view = null; view = list.Views.Add("Items Sorted By Field1", strCol, query, 20, true, false); list.Update();