SP.ListCollection.add Method (sp.js)

Creates a new list or a document library.

Applies to: apps for SharePoint | SharePoint Foundation 2013 | SharePoint Server 2013

SP.ListCollection.add()

Parameters

  • parameters
    Type: SP.ListCreationInformation

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

    It must not be null.

Return value

Type: SP.List

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>

REST resource endpoint

To create a list, send a POST request to the Lists endpoint. See ListCollection resource for more information.