Wednesday, March 3, 2010

How to: Create or Delete Lists

To create a new list, use the one of the Add methods of the SPListCollection class.

The following example adds a new Generic, Events, or Announcements list, based on user input. A Switch clause is used to determine the type of list that the user specifies and sets the type of list template accordingly.



SPWeb mySite = SPContext.Current.Web;
SPListCollection lists = mySite.Lists;

string listTitle = TextBox1.Text;
string listDescription = TextBox2.Text;
string listType = ListBox1.SelectedItem.Text;

SPListTemplateType listTemplateType = new SPListTemplateType();

switch(listType)
{
case "Generic List":
{
listTemplateType = SPListTemplateType.GenericList;
break;
}

case "Events":
{
listTemplateType = SPListTemplateType.Events;
break;
}

case "Announcements":
{
listTemplateType = SPListTemplateType.Announcements;
break;
}
}

lists.Add(listTitle, listDescription, listTemplateType);

No comments:

Post a Comment