SP.ListCollection.add(parameters) Method

Applies to: SharePoint Foundation 2010

Creates a new list or a document library.

var value = SP.ListCollection.add(parameters);

Parameters

Type: SP.ListCreationInformation

Return Value

Type: SP.List

Applies To

SP.ListCollection Class

Exceptions

  • [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 null, ListCreationInformation.Url is not valid, or another list with the title already exists in the site. Error code: -2130575342.
  • The location for list does not exist, the current user has insufficient permissions to perform the operation, ListCreationInformation.Title is null, ListCreationInformation.Url is not valid, or another list with the title already exists in the site. Error code: -2130575342.

    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.

Example

The following example adds an input button on an application page that creates two new announcements lists and adds them to the list collection of the current web site.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">

    var listCollection;
    function runCode() {

        var clientContext = new SP.ClientContext.get_current();
        if (clientContext != undefined && clientContext != null) {
            var web = clientContext.get_web();
            this.listCollection = web.get_lists();

            // Specify the title and template of the new lists.
            var lci1 = new SP.ListCreationInformation();
            lci1.set_title('New Announcements');
            lci1.set_templateType(SP.ListTemplateType.announcements);
            this.listCollection.add(lci1);

            var lci2 = new SP.ListCreationInformation();
            lci2.set_title('Old Announcements');
            lci2.set_templateType(SP.ListTemplateType.announcements);
            this.listCollection.add(lci2);

            clientContext.load(this.listCollection);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
    }

    function onQuerySucceeded() {
        var listInfo = 'Lists on the current site:' + '\n\n';
        var listEnumerator = this.listCollection.getEnumerator();
        while (listEnumerator.moveNext()) {
            var list = listEnumerator.get_current();
            listInfo += list.get_title() + '\n';
        }
        alert(listInfo);
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>

    <input id="Button1" type="button" value="Run Code" onclick="runCode()" />

</asp:Content>

See Also

Reference

SP.ListCollection Methods

SP.ListCollection Properties

SP Namespace