This documentation is archived and is not being maintained.

How to: Create or Delete Lists

Windows SharePoint Services 3

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.

NoteNote

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.

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.

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.

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.

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.

Show: