ListCollection.Add method

Creates a new list or a document library.

Namespace:  Microsoft.SharePoint.Client
Assemblies:   Microsoft.SharePoint.Client.Silverlight (in Microsoft.SharePoint.Client.Silverlight.dll);  Microsoft.SharePoint.Client.Phone (in Microsoft.SharePoint.Client.Phone.dll)  Microsoft.SharePoint.Client (in Microsoft.SharePoint.Client.dll)

public List Add(
	ListCreationInformation parameters
)

Parameters

parameters
Type: Microsoft.SharePoint.Client.ListCreationInformation

A ListCreationInformation object that represents information associated with the list or document library.

It must not be a null reference (Nothing in Visual Basic).

Return value

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

ExceptionCondition
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.

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

        }
    }
}


Show:
© 2014 Microsoft