Compartilhar via


SPListCollection.Add Method (String, String, SPListTemplateType)

Creates a list with the specified title, description, and list definition type.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Overridable Function Add ( _
    title As String, _
    description As String, _
    templateType As SPListTemplateType _
) As Guid
'Usage
Dim instance As SPListCollection
Dim title As String
Dim description As String
Dim templateType As SPListTemplateType
Dim returnValue As Guid

returnValue = instance.Add(title, description, _
    templateType)
public virtual Guid Add(
    string title,
    string description,
    SPListTemplateType templateType
)

Parameters

  • description
    Type: System.String

    A string that contains the description.

Return Value

Type: System.Guid
A System.Guid that identifies the new list.

Remarks

This overload of the Add method applies only to the default Microsoft SharePoint Foundation list templates that are represented by the Microsoft.SharePoint.SPListTemplateType enumeration. To create lists based on other list templates, use one of the other Add method overloads.

Examples

The following code example creates a Contacts list in every site within the current site collection.

Dim site As SPSite = SPControl.GetContextSite(Context)
Dim destSites As SPWebCollection = site.AllWebs
Dim destSite As SPWeb

For Each destSite In  destSites

    Dim destLists As SPListCollection = destSite.Lists

    destLists.Add("New_List_Name", "List_Description", 
        SPListTemplateType.Contacts) 

Next destSite 
SPSite oSiteCollection = SPContext.Current.Site;
using(SPWebCollection collWebsites = oSiteCollection.AllWebs)
{
    foreach (SPWeb oWebsite in collWebsites)
    {
        SPListCollection collList = oWebsite.Lists;
        collList.Add("New_List_Name", "List_Description",
            SPListTemplateType.Contacts);
    }
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See Also

Reference

SPListCollection Class

SPListCollection Members

Add Overload

Microsoft.SharePoint Namespace