다음을 통해 공유


How to: Create or Delete Lists

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

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.

Note

The code examples in this topic use members of the Microsoft.SharePoint.SPContext class to obtain the current site collection, Web site, or list. Outside of an HTTP context, such as in a console application or a Windows application, you obtain references to key objects with a different method. For more information, see Getting References to Sites, Web Applications, and other Key Objects.

Dim mySite As SPWeb = SPContext.Current.Web
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)
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);

The previous example requires a using directive (Imports in Visual Basic) for the Microsoft.SharePoint namespace.

The example instantiates an SPListTemplateType object in order to contain the type of list template that is 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 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. This is shown in the following example, which assumes the existence of a Decision Meetings Workspace site.

Dim mySite As SPWeb = SPContext.Current.Web

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

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

The previous example requires a using directive (Imports in Visual Basic) for the Microsoft.SharePoint namespace.

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 that is based on a specified list template.

Dim siteCollection As SPSite = SPContext.Current.Site
Dim mySite As SPWeb = SPContext.Current.Web

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)
SPSite siteCollection = SPContext.Current.Site;
SPWeb mySite = SPContext.Current.Web;

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

The previous example requires a using directive (Imports in Visual Basic) for the Microsoft.SharePoint namespace.

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.

Dim mySite As SPWeb = SPContext.Current.Web
Dim lists As SPListCollection = mySite.Lists

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

lists.Delete(listGuid) 
SPWeb mySite = SPContext.Current.Web;
SPListCollection lists = mySite.Lists;

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

lists.Delete(listGuid);

The previous example requires a using directive (Imports in Visual Basic) for the Microsoft.SharePoint namespace.

The example assumes the existence of a text box in which the user specifies the name of the list.

See Also

Reference

Microsoft.SharePoint

Concepts

Working with List Objects and Collections

Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio

Security Validation and Making Posts to Update Data

Elevation of Privilege