Export (0) Print
Expand All

SP.ListCollection.add(parameters) Method

SharePoint 2010

Last modified: June 29, 2010

Applies to: SharePoint Foundation 2010

Creates a new list or a document library.

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

parameters

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

It must not be null.

Type: SP.ListCreationInformation

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

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>
Show:
© 2014 Microsoft