in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

MdP's MOSS Blog

You want it to do what?

Creating a List with a Custom View in MOSS

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();

 

Published Feb 26 2007, 03:07 AM by mdp
Filed under: , , , ,

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts