How to: Create or Delete SharePoint Lists

The examples in this topic show how to use the SharePoint Foundation server object model to work with lists in a Web site or site collection.

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

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 an HTTP context, such as in a console application or a Windows application, you obtain references to key objects with a different method. Instead of using an Microsoft.SharePoint.SPContext object, use the SPSite() constructor to instantiate a specific site collection and obtain objects. For more information, see Getting References to Sites, Web Applications, and Other Key Objects.

Creating a SharePoint List with a List Template Type

To create a new list, use one of the Add() method overloads 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.

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 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(String, String, SPListTemplateType) 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.

Creating a SharePoint List with a List Template

In addition to using the SPListTemplateType enumeration to create a list from a template type, you can also create a list from an SPListTemplate object, which represents either a specific list template that has been created in the UI by saving an existing list as a template, or a list schema in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES that defines a list type. 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);

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

You can add a list to multiple Web sites across a site collection, as seen in the following example, which creates a generic list on every Web site, based on the title and description that is passed from two text boxes to the code. The AllWebs property of the SPSite class is used to return the collection of all Web sites that exist on the site.

The example assumes the existence of two text boxes on the .aspx page that contains a form.

Dim listTitle As String = TextBox1.Text.ToString()
Dim listDescription As String = TextBox2.Text.ToString()
Dim mySite As SPSite = SPContext.Current.Site
Dim allWebs As SPWebCollection = mySite.AllWebs

Dim web As SPWeb
For Each web In  allWebs    
    Dim allLists As SPListCollection = web.Lists    
    allLists.Add(listTitle, listDescription, SPListTemplateType.GenericList)
Next web
string listTitle = TextBox1.Text.ToString();
string listDescription = TextBox2.Text.ToString();
SPSite mySite = SPContext.Current.Site;
SPWebCollection allWebs = mySite.AllWebs;

foreach (SPWeb web in allWebs){    
    SPListCollection allLists = web.Lists;
    allLists.Add(listTitle,listDescription, SPListTemplateType.GenericList);
}

Deleting a SharePoint List

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 assumes the existence of a text box in which the user specifies the name of the list.

To delete a list from multiple Web sites, the following example uses nested loops to drill down to a list that has a title matching the title specified in a text box. The example assumes the existence of a text box on the .aspx page that contains a form.

Dim mySite As SPSite = SPContext.Current.Site
Dim allWebs As SPWebCollection = mySite.AllWebs
Dim web As SPWeb

For Each web In  allWebs    
    Dim allLists As SPListCollection = web.Lists    
    Dim i As Integer    

    For i = 0 To allLists.Count - 1        
        Dim list As SPList = allLists(i)        
 
        If list.Title = TextBox1.Text Then            
            Dim listGuid As Guid = list.ID          
            allLists.Delete(listGuid)        
        End If    
    Next i
Next web
SPSite mySite = SPContext.Current.Site;
SPWebCollection allWebs = mySite.AllWebs;

foreach (SPWeb web in allWebs){    
    SPListCollection allLists = web.Lists; 
   
    for (int i=0; i<allLists.Count; i++)    
    {        
        SPList list = allLists[i];        

        if (list.Title == TextBox1.Text)        
        {            
            Guid listGuid = list.ID;            
            allLists.Delete(listGuid);        
        }    
}}

In the example the Title property of the SPList class is used to identify a list in the collection of lists for each Web site that matches the specified title. The ID property returns the globally unique identifier (GUID) of the list, which is passed as the parameter for the Deletemethod.

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

See Also

Reference

Microsoft.SharePoint

Concepts

Working with List Objects and Collections

Using Visual Studio for SharePoint Development

Security Validation and Making Posts to Update Data

Elevation of Privilege