Share via


ListCollection.Add method

Creates a new list or a document library.

Namespace:  Microsoft.SharePoint.Client
Assembly:  Microsoft.SharePoint.Client (in Microsoft.SharePoint.Client.dll)

Syntax

'Declaration
Public Function Add ( _
    parameters As ListCreationInformation _
) As List
'Usage
Dim instance As ListCollection
Dim parameters As ListCreationInformation
Dim returnValue As List

returnValue = instance.Add(parameters)
public List Add(
    ListCreationInformation parameters
)

Parameters

Return value

Type: Microsoft.SharePoint.Client.List
Returns a List instance representing the creation of list or a document library.

Exceptions

Exception Condition
MetadataObjectNotFoundException

ListCreationInformation.Url is not valid.

[Microsoft.BusinessData.MetadataModel.MetadataObjectNotFoundException]

Entity specified in ListCreationInformation.DataSourceProperties or the specified SpecificFinder does not exist on the server. Error code: -1.

[Microsoft.SharePoint.SPException]

Entity specified in ListCreationInformation.DataSourceProperties does not have a View with the specified SpecificFinder. Error code: -2146232832.

SPException

The location for list does not exist, the current user has insufficient permissions to perform the operation, ListCreationInformation.Title is a null reference (Nothing in Visual Basic), ListCreationInformation.Url is not valid, or another list with the title already exists in the site. Error code: -2130575342.

[Microsoft.SharePoint.SPException]

List server template is not valid. Error code: -2130575237.

[Microsoft.SharePoint.SPException]

Feature does not exist. Error code: -2130246262.

UnauthorizedAccessException

The list server template is not valid.

The current user has insufficient permissions. Error code: -2147024891.

Examples

This code example creates two new announcements lists and adds them to the list collection of the current web site.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointFoundation.Samples
{
    class ListCollection_AddExample

    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            Web site = clientContext.Web;
            ListCollection collList = site.Lists;

            ListCreationInformation lci1 = new ListCreationInformation();
            lci1.Title = "New Announcements";
            lci1.TemplateType = (int)ListTemplateType.Announcements;
            site.Lists.Add(lci1);

            ListCreationInformation lci2 = new ListCreationInformation();
            lci2.Title = "Old Announcements";
            lci2.TemplateType = (int)ListTemplateType.Announcements;
            site.Lists.Add(lci2);

            clientContext.Load(collList);
            clientContext.ExecuteQuery();

            Console.WriteLine("Lists on the current site:\n\n");
            foreach (List targetList in collList)
               Console.WriteLine(targetList.Title);

        }
    }
}

See also

Reference

ListCollection class

ListCollection members

Microsoft.SharePoint.Client namespace