Creating or Deleting 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.

[Visual Basic .NET]

Dim mySite As SPWeb = SPControl.GetContextWeb(Context)
Dim lists As SPListCollection = mySite.Lists

Dim listTitle As String = TextBox1.Text
Dim listDescription As String = TextBox2.Text
Dim listType As String = ListBox1.SelectedItem.Text

Dim listTemplateType As New SPListTemplateType()

Select Case listType

    Case "Generic List"

        listTemplateType = SPListTemplateType.GenericList
        Exit

    Case "Events"

        listTemplateType = SPListTemplateType.Events
        Exit

    Case "Announcements"

        listTemplateType = SPListTemplateType.Announcements
        Exit

End Select

lists.Add(listTitle, listDescription, listTemplateType)

[C#]

SPWeb mySite = SPControl.GetContextWeb(Context);
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);

The previous example instantiates an SPListTemplateType object in order to contain the type of list template specified by the user. This object must be passed as a parameter in the Add method. The example assumes the existence of two text boxes where the user can type a title and a description, as well as a drop-down list box that displays the list types for the user to select from.

In addition to using the SPListTemplateType enumeration to create a list, you can also create a list from an SPListTemplate object. The ListTemplates property of the SPWeb class can be used to return a collection of list template objects and a name indexer can be used to specify the list template to use, as in the following example, which assumes the existence of a Decision Meetings Workspace site:

[Visual Basic .NET]


Dim mySite As SPWeb = SPControl.GetContextWeb(Context)

Dim template As SPListTemplate = mySite.ListTemplates("Decisions")
mySite.Lists.Add("My Decisions", "This is a list of decisions", template)

[C#]

SPWeb mySite = SPControl.GetContextWeb(Context);

SPListTemplate template = mySite.ListTemplates["Decisions"];
mySite.Lists.Add("My Decisions", "This is a list of decisions", template);

Using the GetCustomListTemplates method of the SPSite class, the next example returns the custom list templates for a specified site and creates a new list based on a specified list template:

[Visual Basic .NET]

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim mySite As SPWeb = SPControl.GetContextWeb(Context)

Dim listTemplates As SPListTemplateCollection = siteCollection.GetCustomListTemplates(mySite)
Dim listTemplate As SPListTemplate = listTemplates("Custom List Template")
mySite.Lists.Add("Custom List", "A list created from a custom list template in the list template catalog", listTemplate)

[C#]

SPSite siteCollection = SPControl.GetContextSite(Context);
SPWeb mySite = SPControl.GetContextWeb(Context);

SPListTemplateCollection listTemplates = siteCollection.GetCustomListTemplates(mySite);
SPListTemplate listTemplate = listTemplates["Custom List Template"];
mySite.Lists.Add("Custom List", "A list created from a custom list template in the list template catalog", listTemplate);

To delete a list, you must specify the GUID of the list as the parameter for the Delete method. Use the ID property of the SPList class to find the GUID.

[Visual Basic .NET]

Dim mySite As SPWeb = SPControl.GetContextWeb(Context)
Dim lists As SPListCollection = mySite.Lists

Dim list As SPList = lists(TextBox1.Text)
Dim listGuid As System.Guid = list.ID

lists.Delete(listGuid) 

[C#]

SPWeb mySite = SPControl.GetContextWeb(Context);
SPListCollection lists = mySite.Lists;

SPList list = lists[TextBox1.Text];
System.Guid listGuid = list.ID;

lists.Delete(listGuid);

This example assumes the existence of a text box where the user specifies the name of the list.